1

In Android, from Activity A, if I start new Activity B, Android will automatically save all the state of Activity A. So when I click back or call finish() from Activity B, Activity A will be restored to the state when I start Activity B (for example: position of a ScrollView in Activity A, or the value of ProgressBar). In Activity A I have a ProgressBar showing progress of some task, then if I come back from Activity B, the ProgressBar is restored and keep running.

Now what I want is, from Activity A, I will call finish(). How can I save all the state of Activity A just like Android did (include the ProgressBar as I describe above), so when I start Activity A again, I can restore everything like before calling finish(), and my ProgressBar keep running?

Can I push Activity A to something like stack in Android and then pop it out? Anyone know how to achieve this?

user1417127
  • 1,525
  • 1
  • 21
  • 29

4 Answers4

3

Do not call finish on clicking the back button.

Instead call the method "moveTaskToBack(false)". This will 'minimize' your application. Next time you open the application the application opens with the previous state.

Umesh
  • 4,406
  • 2
  • 25
  • 37
  • 3
    This should not be heavily relied on due to the OS or applications that may kill background processes. Data should still be saved and passed to new instances if it is to be retained. – Cat Jul 10 '12 at 04:52
  • Activity A is started from another Activity, for example Activity Z. So when I call finish, it will go back to Activity Z. Now can I call moveTaskToBack(false) in this situation? – user1417127 Jul 10 '12 at 04:53
  • Thats right @Soxxeh. Using onSaveInstanceState() and onRestoreInstanceState() is the way to go. The combination of moveTaskToBack(false), onSaveInstanceState() and onRestoreInstanceState() should do the trick. – Umesh Jul 10 '12 at 04:55
  • @user1417127 You mean Activity A is not part of your application. You are just invoking it via an intent? If that is the case i dont think there is a way to achieve what you want because Activity A is not your activity and you do not have access to its code. – Umesh Jul 10 '12 at 05:00
  • No Activity A is part of my application. Let me make this clear: When app start, it start Activity Z. Activity Z have a ListView. Each item of ListView will start Activity A with different parameters. If Activity A finish, application go back to ListView of Activity Z. Now if I click the same row I've clicked before, I want it restore the state. – user1417127 Jul 10 '12 at 05:54
  • In that case you will have to probably map the state of the activity to the list items. create an object that saves the state of the Activity A. Use 1 of the Map object and save the activity state with list item index as your key. Upon selecting a list item populate your activity A with the saved state values. – Umesh Jul 10 '12 at 06:17
  • Thanks. Not exactly solve my problem but your answer is closest. So I'll set this as answer – user1417127 Jul 11 '12 at 04:28
0

The whole point of finish() is to say that your Activity is "done and should be closed". If you aren't done, just don't call finish().

Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
  • Even if I don't call finish(), when I start Activity A again from another Activity, how can I restore the state of Activity A because it will start Activity A with default value. – user1417127 Jul 10 '12 at 04:46
  • 2
    @user1417127 Sorry, I assumed you were familiar with the [Activity lifecycle](http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle), but I shouldn't have. That link and [this one](http://developer.android.com/guide/components/activities.html#SavingActivityState) explain it in detail. The short version is to override `onSaveInstanceState()` and `onRestoreInstanceState()`, but you should read those articles for the details. – Darshan Rivka Whittle Jul 10 '12 at 04:50
0

When you go from Activity A to Activity B, Android does not kill Activity A. Activity A still lives in the background. It can however be killed at any moment if Android needs the memory.

To save the state of an activity you have to do it manually.

Here's a similar question with a good answer: Saving Android Activity state using Save Instance State

Community
  • 1
  • 1
hatcyl
  • 2,190
  • 2
  • 21
  • 24
  • Ya I know Android does not kill Activity A. My point is can I save Activity A like Android did? Then when I start Activity A from another Activity, I just need to restore it – user1417127 Jul 10 '12 at 04:47
0

If you want to finish your activity and then start it again just to load some new configurations, You can call activity's recreate() method instead of calling finish(). Calling recreate() will call onSaveInstanceState() and onRestoreInstanceState(), helping you to restore your previous configurations..

You can look at this answer for more detail : How do I restart an Android Activity

Community
  • 1
  • 1
Vijay
  • 439
  • 3
  • 15