问题
There can be many activities in application and the last launched activity stay on top of stack and on pressing back it finish the current activity.I have a sequence of Activity and here is the flow ..
if we have A,B,C(1),D,C(2)... Activity C(1) and C(2) are two different instance of Activity C launched while navigating the application .So what is requisite is to clear all the instance of activity C and the result should be when I finish C(2) I should have left with these stack A,B,D. What should I do .
IMP -I want to keep the C(1) alive in stack until unless I finish the C(2) as I could have started C with New Task flag rather than creating these instances , but these instance have different UI and work .
The following approaches are not favorable .
First
@Override
public void onBackPressed(){
super.onBackPressed();
Intent intent = new Intent(C(2).this , D.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
startActivity(intent);
}
This will clear All the activity from Stack and relaunch Activity
Second
Keep the track of Activity in singleton class and then relaunching the required flow, how ever this will consume the time when there are many Activities to be started .
So I thought that there should be some solution using package manager or other that will resolve the problem , solution are appreciated
回答1:
I don't know a way to close activity C1 manually when finishing activity C2.
However you can take care of activity C1 in it's on resume this way -
1 - Set a flag in your application class :
public static boolean IsClosingActivities = false;
This value can be set "true" by C2 right before "finish" of activity C2 occurs.
And set "false" in the point where you'll call startActivity for a new activity C. (Assuming that new instances of activity C can be created later in the application).
2 - In C implementation of on resume :
@Override
protected void onResume() {
super.onResume();
if (YourApplication.IsClosingActivities) {
this.finish();
}
}
This way - when the user navigates back from D - C1 will finish itself and he will be navigated to B.
来源:https://stackoverflow.com/questions/14766929/finish-all-instance-of-particular-activity