Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear activity history stack when pressing home button?

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).

like image 958
dbm Avatar asked Jan 06 '12 12:01

dbm


People also ask

How do I delete activity from Stack?

The easiest way is to give the LoginActivity a “android:noHistory = true” attribute in the manifest file. That instructs Android to remove the given activity from the history stack thereby avoiding the aforementioned behavior altogether.

How do I get activity stack?

This example demonstrate about How to get top activity name in activity stack. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is finishAffinity?

finishAffinity() : finishAffinity() is not used to "shutdown an application". It is used to remove a number of Activities belonging to a specific application from the current task (which may contain Activities belonging to multiple applications).


1 Answers

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);
like image 72
Martyn Avatar answered Oct 14 '22 01:10

Martyn