android multiple instance of activity?
I have a question regarding activity multiple instance.
eg)
A开发者_Go百科1 is an activity, A1 starts A2 using startActivity(), then A2 starts A1 using startActivity() as well, how many instance of A1 exists in current system?
AS you asked there will be only one instance of A1 will be present.
- If you didnot finish A1 while going to A2 then new instance will be created while coming back to A1.
- If you didnot finish then the background running instance of A1( which will be in stack) will come to the front.
here is example
Intent i=new Intent(A1.this,A2.class);
startActivity(i);
finish();
This is my point 1.In this case if you will come back from A2 then new instance will be created.
Here is my second point just without finish();
Intent i=new Intent(A1.this,A2.class);
startActivity(i);
In this case if u will come back from A2 then A1 is already being running in background because you have not finished it while going to A1.So it will come to front.
It depends on the flags you set on the intent when you start the activity. If for example you set them to Intent.FLAG_ACTIVITY_REORDER_TO_FRONT, then the activity will be reused, i.e. no multiple instances. If you don't set any flags, then the scenario you describe will launch a new activity on the stack. Eventually you'll run out of memory. Only calling finish() will destroy the activity instance
精彩评论