launchModes, singleTask: how to switch between "screens"? Is Intents the only way, and if so I think I have a problem

In a number of questions (like this one) I have been looking into how to "change screens" in my app. I have a "header" on top, with 4 buttons. Each button is meant to replace the "content" (ie change screen):

| menu with buttons  |
|                    |
|                    |
|  C O N T E N T     |
|                    |
|                    |

When I click a Menu button, I run the following code:

public void onClick(View v)
    Intent myIntent = new Intent(BaseActivity.this, ActivityMain.class);

As you can see, the startActivity is executed. Now, if I do not specify "launchMode" for the Activity that means that launchMode = normal. If launchMode == normal that means that the Activity will be re-created each and every time I navigate using the top header buttons, and that means that all data entered in "form elements" are gone (or at least hidden).

So, I found the launchMode "singleTask" that sounded sort of nice. If I add that launchMode to my Activity, it will not be re-created when I navigate with the buttons, thus keeping state. Great! Well, until I read this:

As noted above, there's never more than one instance of a "singleTask" or "singleInstance" activity, so that instance is expected to handle all new intents.

I found out that the sentence mean that there can be only one Activity that has the launchMode set to "singleTask" - if I have more than one it wont work (no compiler error though).

This means that I can only "keep the state" for one Activity, when switching back and forth (navigating) between my screens!

Once again, is this really how it supposed to work?

Asked by: Paul608 | Posted: 20-01-2022

Answer 1

You seem to be forcing activities to exist where they shouldn't.

If you are going to use buttons as faux tabs, then there should be one activity, no startActivity() calls, and use ViewFlipper or a FrameLayout or something to change your content to match the button. After all, this is how tabs work, and just because you are electing not to use TabHost/TabActivity but roll your own tab-esque system, the design approach should be the same.

Answered by: Kevin974 | Posted: 21-02-2022

Answer 2

As noted above, there's never more than one instance of a "singleTask" or "singleInstance" activity, so that instance is expected to handle all new intents.

that means one instance of activity where you've set your "singleTask" or "singleInstance". if you have two activities with that setting it's going to mean you have only two instances. basically set it on as many activities as you like as long as you understand what you're doing.

Answered by: Sawyer864 | Posted: 21-02-2022

Answer 3

You are trying to solve your problem the easy way but what about when the user changes orientations and your activity is restarted? Won't that wipe out the form? Try it. I think you need to look at saving the state of the form in the bundle. With Android, your activities need to assume that they can be restarted at any time. See below SO post:

Saving Android Activity state using Save Instance State

Answered by: Miller760 | Posted: 21-02-2022

Similar questions

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android