Using previous running activity instead of starting a new one

六眼飞鱼酱① 提交于 2020-01-24 01:08:25

问题


it is possible to Use previous running activity instead of starting a new one without affecting the back stack?

for example activity A -> B -> C -> A i want to achieve that the system will use Activity A instanse without starting a new one, and without affecting the back stack.

so when user will click Back he will travel the original path and the last activity will be A not B, by using simply singleTop/ReorderToFront flag i will be able to use the original activity but i will lose the back stack

i will like to achive a "browser like experience" so every time the user will click back he will back to his previous page the case can be much more complex then that for example

A -> B -> C -> A -> B ->B -> C -> D -> A etc...


回答1:


If you want to emulate the behaviour of a browser, then you should just allow Android to create new instances of the activities, which it will do. The user can then press BACK to navigate back through the list of activities.

You can't reuse existing instances and rearrange them and still maintain the backstack, because when Android moves an Activity from wherever it is in the stack to the front (which you can do using FLAG_ACTIVITY_REORDER_TO_FRONT) it removes it from where it was in the back stack.

If you really want to reuse existing instances and maintain the back stack then you will have to implement this yourself:

Create a variable static ArrayList<Class> stack that you use as a stack to remember which Activity was used at what point in the navigation. Everytime you launch an Activity you should us startActivity() and make sure that you set FLAG_ACTIVITY_REORDER_TO_FRONT so that an existing instance will be moved to the front. When you call startActivity()you must also push theClassinstance of theActivity` onto your stack. This allows you to keep track of which activities where launched in what order. This works all fine going forwards. Now the tricky part comes when the user presses BACK.

Override onBackPressed() in each Activity. When onBackPressed() is called, do this:

// pop my Activity off the stack
Class myClass = stack.remove(stack.size() - 1);
// Check if there are any more instances of my Activity in the stack
//  Don't finish this instance if the instance exists in the stack
if (!stack.contains(myClass)) {
    // There are no more instances of my Activity in the stack, so
    //   finish this instance
    finish();
    // Check if this is the root Activity, if so we are done!
    if (stack.size() == 0) {
        return;
    }
}
// Get the Class representing the previous Activity from the top of the stack
Class activityClass = stack.get(stack.size() - 1);
// Launch that Activity
Intent launchIntent = new Intent(this, activityClass);
launchIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(launchIntent);

This will pop the current Activity off the stack, finish the Activity if there are no more instances of it in the stack, get the previous Activity from the top of the stack and launch it bringing it to the front. This gives the illusion you are looking for.



来源:https://stackoverflow.com/questions/52424992/using-previous-running-activity-instead-of-starting-a-new-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!