2

I have three top level activities in my application. Activity A, B & C.

Each one of these activities hosts a navigation drawer. I am trying to figure out the best way to manage the activity stack between these three activities.

For example, When I start the application, Activity A is launched. Activity A has a navigation drawer like Activities B & C. When I click on Activity B in the drawer, Activity B is launched and clicking on Activity C in the drawer launches Activity C etc...

I don't want to finish these Activies when the drawer launches a new Activity because they load data from a backend service, and when I click the back button I want it to send the application to the background.

Essentially, I am looking for a way to launch the activity if it does not exist, and if it does, just resume it. How can I accomplish this?

Nick H
  • 8,897
  • 9
  • 41
  • 64
  • 2
    1) Store data in the database. There's no guarantee Android will keep your data for long 2) Consider using only one Drawer and put A, B & C as Fragments. – Sharjeel Jan 26 '16 at 03:42
  • @Sharj Thanks for the comment. For #1, yes that is a consideration, but that will all be handled inside onCreate() if that activity is destroyed, #2 This is about Activities, not fragments. It is good advice though if that particular use-case works for someone else though. – Nick H Jan 26 '16 at 03:53

1 Answers1

0

I think decoupling retrieving data from the activity is the best option.

The following paragraph is from Tasks and Back Stack:

Because the activities in the back stack are never rearranged, if your application allows users to start a particular activity from more than one activity, a new instance of that activity is created and pushed onto the stack (rather than bringing any previous instance of the activity to the top). As such, one activity in your application might be instantiated multiple times (even from different tasks), as shown in figure 3. As such, if the user navigates backward using the Back button, each instance of the activity is revealed in the order they were opened (each with their own UI state).

So in your case, grabbing the data in the background when the app starts using async tasks and storing them in the database might work out better.

One way to do it would be:

On create of the home activity, quickly grab the home activity's data via async task while showing a progress bar. When done, store it, and display it. Then, launch async tasks for the data for other activities. There are some conditions that could be tricky. For example, you have to make sure you show a progress bar if the user quickly switches to Activity B or C before your data is ready.

Perhaps using a singleton might suite your needs if you do not want to use the DB. Depending on the size of your data, parceling your data and passing it through a bundle might also prove to be a good technique.

alotofquestions
  • 164
  • 1
  • 2
  • 12