asprin asprin - 29 days ago 13
Android Question

Any consequences of startActivity() after having already started the activity?

Let's say I have Activity A and Activity B.

In A, I have a button which when clicked runs the following code

startActivity(new Intent(this, ActivityB.class));


In B, I have a button which when clicked, runs the following code

startActivity(new Intent(this, ActivityA.class));


So when the app is started and the user does the following:


  • Clicks on button in Activity A (he goes to Activity B)

  • Clicks on button in Activity B (he comes back to Activity A)

  • Again clicks on button in Activity A (goes to Activity B again)



In this case, do the Activities A and B hog the memory since they get started each time or is Android smart enough to know that an activity has already started and simply needs to be put on top of the stack?

So in other words, if memory does indeed gets clogged up, is there something like:

if(activity has already started)
{
doNotStartActivityButSimplyPutItAtTop();
}
else
{
startActivity(new Intent.....);
}

Answer

yes, your assumption is right android is capable of managing these activities and they are maintains proper stack for it.

if you are having only two activities you can use singletop launcher mode so that only only two activities remain in stack.