开发者

How to clear specific activity from the stack history?

Suppose I have an application containing activities named A,B,C,D. Now, consider A has been launched as the root activity and B has been launched from A and C has been launched from B and D has been launched from C. Now I have a button named "Remove" in the activity D. If suppose, I press the button "Remove" in the activity D, then the activity B and activity C should be removed from the history stack. 开发者_高级运维On Pressing back key from the activity D should display activiy A instead of C. I don't know how to implement this. Can anyone help me to resolve this problem?


I'm not sure you can directly programmatically remove activities from the history, but if you use startActivityForResult() instead of startActivity(), then depending on the return value from your activity, you can then immediately finish() the predecessor activity to simulate the behaviour you want. By using this method in all your activities, you can have this behaviour cascading the activity stack to allow you to go from activity D to activity A.

I know this isn't your situation, but in future if you know before you start the activity that you don't want the predecessor to remain, you can call finish() immediately after startActivity().

Please see the section called "Lifetime of the New Screen" in Common Tasks and How to do Them in Android


I agree with @RivieraKid, but I think of another way: When you press "Remove" you set a custom flag to true.

Override the back key event:

public void onBackPressed() {
  if (!remove){
    super.onBackPressed();
  }else{
    Intent goToA = new Intent((this,ActivityA.class););
    goToA.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(goToA);
  }
}

Do you think this does what you want?

Good luck.


RivieraKid has the best answer (and a cool login), but for completeness, I'd like to mention that you can also set the FLAG_ACTIVITY_NO_HISTORY flag in an intent, to cause the newly-launched activity to not appear in the history. See http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NO_HISTORY


When you launch the application. All the activities are launched in the same task by default. So the back stack looks like as below.

Task: A->B->C->D in this case the TOP of back stack is pointing to "D"

Now when if you want to make the TOP point to "A" again. Just declare the attribute

android:launchMode="singleTask"

for the activity "A" in your AndroidManifest.xml

Now when you press the "remove button" in activity "D" just start the activity as usual as you do. The back stack is cleared of the activity "B" and "C" and You get a clean start from activity "A"

Remember this will close and destroy your activity D from the memory even if you do not call finish() explicitly in "D"


I think this would do what you want. In the manifest file just add the "noHistory" attribute to the "B" and "C" activities that you don't want revisit upon pushing the back button in "D".

    <activity   android:name="com.acme.activityA"></activity>

    <activity   android:name="com.acme.activityB"  
                android:noHistory="true"></activity>

    <activity   android:name="com.acme.activityC"  
                android:noHistory="true"></activity>

    <activity   android:name="com.acme.activityD"></activity>


An alternative is create a class to have access to the activities and close them. Example:

Activities:

  • act1
  • act2
  • act3

Control class:

public class ActivitiesControl {

    public static act1 vAct1 = null; 
    public static act2 vAct2 = null;
    public static act3 vAct3 = null;
}

In the onCreate of each activity you set: ActivitiesControl.vAct1 = this;

and onDestroy(): ActivitiesControl.vAct1 = null;

When you want to destroy some activity to remove it from the stackHistory you just call:

ActivitiesContro.vAct1.finish();

I don't know if this is the best method, but is what worked for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜