切换 Activity 实例的最简单方法
我有很多相同活动的实例。 设此时的栈为A1->A2->A3->A4->A5,其中A5在最上面。 我启动下一个活动的代码是这样的:
Intent intent = new Intent();
intent.putExtra("some option", "its value");
intent.setClass(this, MyActivity.class);
startActivity(intent);
我需要有机会将任何实例中的每个实例置于前面。例如我想恢复(!)A2。如果可以对活动堆栈重新排序,那就太好了,我认为如果可能的话,这是最简单的方法。 我认为使用标志并不是最简单的方法,并且不能完全解决问题。 我想创建一个类来轻松切换实例,但我想首先手动完成。 我尝试了很多方法..这是一种。
Intent intent = new Intent(A2, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
A2 是我的活动类(类型 - 活动)的静态变量。它包含指向我的 A2 实例的指针。 A 也有 A1 - A5 变量,如 A2。 我已将 Log.e 添加到 onResume 函数中。我看到我的代码将 A5 带到前面,而不是 A2 (没有日志,看起来我的代码什么也没做)。
请帮助我:)
I have many instances of same activity.
Let the stack at the moment is A1->A2->A3->A4->A5, with A5 in the top.
The code I launching ever next activity is like that:
Intent intent = new Intent();
intent.putExtra("some option", "its value");
intent.setClass(this, MyActivity.class);
startActivity(intent);
I need to have opportunity to bring to front every instance from any instance. For example I want to resume(!) A2. And it will be very good if it is possible to reorder activity stack, I think it is the most simple way if it's possible.
I think the using flags is not the most simply way, and they can not solve the problem fully.
I want to make class for easy switching instances, but I want to do it manually for the first.
I tried many ways.. here is one.
Intent intent = new Intent(A2, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
A2 is static variable of my activity class (type - Activity). It contains pointer to my A2 instance. A have also A1 - A5 variables like A2.
I have added Log.e to onResume function. And I see that my code brings to the front A5 instead A2 (without log it looks like my code does nothing).
Help me please :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以建议您将目录页面移动到片段而不是活动中吗?这样,您就可以创建 Fragment 的多个实例,每个实例都具有唯一的标签,并可以随意
add()
或remove()
它们。更多信息请访问 Android 开发网站此处和这里。
如果您使用的是 Android 3.0 以下的版本,请记住您可以使用兼容性包来获取 片段支持。
Can I suggest that you move the catalog pages into a Fragment instead of an Activity? That way you can make multiple instances of the Fragment, each with unique tags and
add()
orremove()
them at will.More information can be found at the Android Dev site here and here.
If you are working with something below Android 3.0, remember that you can use the compatibility package to get Fragment support.