How To Enable The Home Button To Return To A Common Activity?
Solution 1:
I'd consider using the single instance launchmode for that activity.
AndroidManifest.xml
<activityandroid:launchMode="singleInstance">
...
</activity>
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?"