My question is really short. I have a game which has a reset class that resets all the objects. If I understand it correctly I only need one instance of the game activity at the same time. Since if Android created multiple instances of the activity it would be a waste of resources.
开发者_Go百科Is the changing of launchMode in Android a way to achieve this. Is it safe to use launchMode="singleInstance" to achieve this?
Check out the answer at Android singleTask or singleInstance launch mode? - which does a good job if explaining the documentation and refers on to an excellent blog / android app (at http://blog.akquinet.de/2010/02/17/android-activites-and-tasks-series-an-introduction-to-androids-ui-component-model/).
I am not sure what launchMode="singleInstance" does, but you could achieve what you are trying to do by using the Singleton Pattern:
http://www.javabeginner.com/learn-java/java-singleton-design-pattern
I think singleTop is what you are looking for. singleTask and singleInstance should only be used in rare cases.
If i'm right, singleTop
will do the thing only if you have only 1 Activity
so it's always on the top of the stack. Otherwise you can try to use Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
flag while sending an intent.
精彩评论