Issue
I'm writing an application which have two "modes"; one view mode and one edit mode. The application exposes two main/launcher activities (say, A and D) which in turn have their own activity chains (B, C and E, F respectively). The two entry point activities A and D will expose two icons with separate labels in the home screen and the two chains are never crossed, i.e. once you start the application in a view mode with activity A, the only route you can walk back and forth is A, B, C and if you start the application in edit mode with activity D, the only available activity chain is D, E, F.
Now, my problem is that if start the application in, e.g. view mode (activity chain A, B, C) and press the Home button from any activity in that chain I get back to the home screen (of course) but if I then re-start the application in edit mode (activity chain D, E, F) I get to the activity I was on when pressing the Home button (that is, an activity in the wrong chain) - not the expected entry point for edit mode; activity D.
How do I solve this?
I have tried various combinations of android:noHistory
, android:clearTaskOnLaunch
and other attributes in AndroidManifest.xml
for the involved activities. But they only seem to affect the very activity, not the entire chain.
I would like to remove the entire chain of activities (A, B, C or D, E, F) from the history stack when the Home button is pressed but still keep the stack intact while I'm still in the chain (I want to be able to press the back button from, say, activity B and get to activity A).
Solution
Sounds like you need to use the Intent.FLAG_ACTIVITY_CLEAR_TOP
flag on your home activities, but of course you can't add these flags in the AndroidManifest.xml file. Maybe you should have a single point of entry which then launches the correct Activity
- you can use an activity-alias to make it look like two points of entry to the user.
For example - you define the activities in your manifest file:
<activity-alias
android:label="@string/edit_app_name"
android:name="launch_edit"
android:targetActivity=".activities.LaunchActivity">
<meta-data android:name="launch_type" android:resource="@string/launch_edit" />
</activity-alias>
<activity-alias
android:label="@string/view_app_name"
android:name="launch_view"
android:targetActivity=".activities.LaunchActivity">
<meta-data android:name="launch_type" android:resource="@string/launch_view" />
</activity-alias>
Then in your LaunchActivity you have:
ActivityInfo activityInfo = getPackageManager().getPackageInfo( this.getComponentName(), PackageManager.GET_ACTIVITIES|PackageManager.GET_META_DATA);
int launchTypeResource = activityInfo.metaData.getInt("launch_type");
String launchType = context.getString(launchTypeResource);
if(launchType == null) {
// handle error
throw new Exception();
}
Intent newIntent;
if(launchType.equals(context.getString(R.string.launch_view)) {
newIntent = createIntent(ViewActivity.class);
} else if(launchType.equals(context.getString(R.string.launch_edit)) {
newIntent = createIntent(EditActivity.class);
}
newIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(newIntent);
Answered By - Martyn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.