如何限制堆栈中的同一活动的人数为Android应用程序 [英] How to limit the number of the same Activity on the stack for an Android application

查看:113
本文介绍了如何限制堆栈中的同一活动的人数为Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是有可能在一个Android应用程序?我希望让这个无论多少次,用户开始activityA,当他们击中背部按钮,他们将永远不会activityA的多个occurence。

Is this possible in an Android app? I want to make it so that no matter how many times a user starts activityA, when they hit the back button they will never get more than one occurence of activityA.

什么我发现在我目前的code是,我只有两个选择: 1.我可以调用finish()的activityA将prevent它是完全通过后退按钮访问,或 2.我不叫结束(),然后,如果用户的使用期间启动activityA(n)的时间,将有(n)的实例击中后退按钮时。

What I am finding in my current code is that I have only two options: 1. I can call finish() in activityA which will prevent it from being accessible via the back button completely, or 2. I do not call finish(), and then if the user starts activityA (n) times during their usage, there will be (n) instances when hitting the back button.

此外,我想有activityA访问通过点击返回按钮,但没有理由继续在堆栈上的同一个活动的多个实例。是有办法在队列中的一个活动的实例的数量限制为只有1?

Again, I want to have activityA accessible by hitting the back button, but there is no reason to keep multiple instances of the same activity on the stack. Is there a way to limit the number of instances of an activity in the queue to only 1?

推荐答案

一种选择是使用 Intent.FLAG_ACTIVITY_REORDER_TO_FRONT 每次推出一个活动,这样,如果一个实例存在时间它被引入到堆叠的前部,而不是在每一次。 在确保这样你,只有一个活动将继续留在堆栈中。

One option is to use Intent.FLAG_ACTIVITY_REORDER_TO_FRONT every time you launch an Activity so that if an instance exists it is brought to the front of the stack and not created every time. This way you are ensured that only one Activity will remain on stack.

这篇关于如何限制堆栈中的同一活动的人数为Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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