使用上一个正在运行的活动,而不是开始一个新的活动 [英] Using previous running activity instead of starting a new one

查看:56
本文介绍了使用上一个正在运行的活动,而不是开始一个新的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用先前正在运行的活动而不是开始新的活动而不影响后退堆栈?

例如活动A-> B-> C-> A 我想实现系统将在不启动新活动的情况下使用活动A实例,而又不影响后退堆栈.

因此,当用户单击后退"时,他将走过原始路径,最后一个活动将是A而不是B,只需使用singleTop/ReorderToFront标志,我就可以使用原始活动,但我会丢失后退堆栈

我希望获得浏览器般的体验",因此每次用户单击时,他将返回上一页,情况可能会比例如

复杂得多.

A-> B-> C-> A-> B-> B-> C-> D-> A等...

解决方案

如果要模拟浏览器的行为,则应只允许Android创建活动的新实例,它将执行此操作.然后,用户可以按BACK返回活动列表.

您不能重用现有实例并重新排列它们,而仍要维护后退堆栈,因为当Android将Activity从堆栈中的任何位置移到前端(您可以使用FLAG_ACTIVITY_REORDER_TO_FRONT进行操作)时,它将从其中删除它它在后面的堆栈中.

如果您真的想重用现有实例并维护后台堆栈,则必须自己实现:

创建一个变量static ArrayList<Class> stack,将其用作堆栈,以记住在导航中的哪个位置使用了哪个Activity.每次启动Activity时,都应该使用startActivity()并确保设置FLAG_ACTIVITY_REORDER_TO_FRONT,以便将现有实例移到最前面.当您在堆栈上调用startActivity()you must also push the Class instance of the Activity`时.这使您可以跟踪以什么顺序启动了哪些活动.这一切正常.现在,当用户按下BACK时,棘手的部分就会出现.

在每个Activity中覆盖onBackPressed().调用onBackPressed()时,请执行以下操作:

// 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);

这将从堆栈中弹出当前的Activity,如果堆栈中没有更多实例,请完成Activity,从堆栈顶部获取前一个Activity并将其启动前方.这给出了您要寻找的错觉.

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...

解决方案

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.

这篇关于使用上一个正在运行的活动,而不是开始一个新的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆