简体   繁体   中英

Android - How to limit activity instances to one per list item?

I have FooListActivity (one instance, showing a list of all the foos) and FooActivity (zero or one instance per foo).

FooActivities can be started from the FooListActivity, but also from a long-running service with no user intervention.

How can I make sure that, at most, one FooActivity exists per foo?

ie If I try to start a FooActivity for a foo which already has a FooActivity, it should bring the existing FooActivity to the front, rather than creating an unneeded instance.

If I understand you correctly you do not want just a single instance of your activity (which you can control with android:launchMode="singleInstance" ), but you want single instance per displayed content? If so, you will have to keep track of this yourself.

Also, I am not sure this is a good idea at all. While I can imagine some usage patterns for this on tablet (but then with Fragments, not Activities), then on phone user need to get back to list to see another data anyway (and if setting up FooActivity to display data is heavy, then you should perhaps think of some caching instead?

请为FooActivity设置一个标志:

android:launchMode="singleInstance"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM