Skip to content Skip to sidebar Skip to footer

How To Enable The Home Button To Return To A Common Activity?

I use ActionbarSherlock and would like to enable the home button ... Therefore I call setHomeButtonEnabled(true) in my base activity. public class BaseFragmentActivity extends Sher

Solution 1:

I'd consider using the single instance launchmode for that activity.

AndroidManifest.xml

<activityandroid:launchMode="singleInstance">
...
</activity>

Reference

Only allow one instance of this activity to ever be running. This activity gets a unique task with only itself running in it; if it is ever launched again with the same Intent, then that task will be brought forward and its Activity.onNewIntent() method called. If this activity tries to start a new activity, that new activity will be launched in a separate task. See the Tasks and Back Stack document for more details about tasks.

Solution 2:

In the Android documentation I found what I was searching for: You can set the flag FLAG_ACTIVITY_CLEAR_TOP to clear the back stack. The second flag FLAG_ACTIVITY_SINGLE_TOP avoid restarting the activity if used in combination with the flag mentioned before.

case android.R.id.home: {
    Intent intent = newIntent(this, HomeActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivityIfNeeded(intent, 0);
    returntrue;
}

The intent needs to be passed using startActivityIfNeeded().

Post a Comment for "How To Enable The Home Button To Return To A Common Activity?"