views:

234

answers:

1

Hi, i have the following question.

I have an activity which is showing a progress bar while a service is downloading data from an API. I want that when i press Home and relaunch my program the activity and which is not the first activity called but it is in the stack was recovered in order to continue showing the progress.

I have read about the cycle of life of activities but i don't find a clear solution.

The same thing happens If i am in the activity which is showing the progress bar and i press a button that takes me to another activity, when i go back, can i recover the old activity instead launch one new?

Anyone helps me?

Thanks

+2  A: 

The problem is that pressing the home button will erase the whole activity stack. That means there is no possibility to go back to the activity it even is not certain that the activity still exists.

If this a progress that is interesting for the user that it is still running you could display a notification bar icon until the progress is finished. I think you can specify a special intent for clicking on the notification bar and filter this intent with your activity. That way you would go back to the activity. But you still face the problem that the activity is saved and has no reference to the background thread doing the work.

If your Activity has left the stack its finish method is called. You shouldn't try to reuse this activity later on. The best way is to think of a way that the whole state of the activity can be saved and restored later on. To restore a reference to the background thread doing the work you could subclass the application class and save a reference to the running task in your subclass.

Janusz
the activity still exists because the service is sending data to it so i think it should be possible to recover it
xger86x
the activity will exist until the service is completed and the garbage collector can clean the activity up.
Janusz
ok, so while the service is still running, if a relaunch my application, can i take that instance of the activity in the stack?
xger86x
Or the same thing. If i am in the activity which is showing the progress bar and i press a button that takes me to another activity, when i go back, can i recover the old activity instead launch one new?
xger86x
I think after leaving the stack the activity should not be reused... Edit my answer
Janusz