Start Activity clearing top in Android

2019-02-28 21:47发布

问题:

I have the following Activities in my stack:

A , B , C, D 

I want to relaunch B in order to get this stack:

A , B'

Where B' is a new B instance (not the old receiving a onNewIntent callback, how can I do it?

By the way I'm using a

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

when launching the B activity but this way the onNewIntent is called instead of start a new instance

回答1:

Use android:noHistory=true in manifest file for particular activity to clear.Hope this might solve your issue.



回答2:

here when you use this code the its take you on activity B. in B you Press backbutton the its take you on A.

Intent fromDtoB = new Intent(this,B.class);
fromDtoB.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(fromDtoB);

FLAG_ACTIVITY_CLEAR_TASK: If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started.

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