[ACCEPTED]-Go back to specific activity from stack-activity-manager

Accepted answer
Score: 32

Assuming that Activity2 is the second activity that 8 you want to go,

Try this:

Intent intent = new Intent(this,Activity2.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

According to Android Documentation about 7 FLAG_ACTIVITY_CLEAR_TOP

If set, and the activity being launched 6 is already running in the current task, then 5 instead of launching a new instance of that activity, all 4 of the other activities on top of it will 3 be closed and this Intent will be delivered 2 to the (now on top) old activity as a new 1 Intent.

Score: 13

Skip Activity by Manifest attribute

It depends from the need but if We simple 14 want just skip activity in back flow, then 13 helpful can be removing this activity from 12 history in Manifest.

[1] -> [2] -> [3] - normal 11 flow

[1] <- [3] - back flow

Then for [2] Activity 10 We can in Manifest set noHistory attribute:

<activity
android:name=".SecondActivity"
android:noHistory="true" />

Thanks 9 this approach our [2] Activity will never 8 be launched in back flow.


Use Intent Flag

Removing activity 7 from history stack is not always good idea, for 6 example if our Activity sometimes need to 5 be on back flow and sometimes not, then 4 for launching wanted activity We need to 3 set flag in intent:

Intent intent = new Intent(this, FirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

And very important - in FirstActivity manifest set launch 2 mode on singleTop.

<activity
   android:name=".FirstActivity"
   android:launchMode="singleTop" />

Without launchMode attribute activity 1 will be re-created.

Score: 0

use this

Intent intent = new Intent(this,Activity2.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

0

Score: 0

For KOTLIN

        val intent = Intent(this, yourDestinationActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        startActivity(intent)

0

More Related questions