0


I was reading this tutorial http://developer.android.com/training/basics/activity-lifecycle/stopping.html about stopping and restarting an activity and I ran into a doubt, the text says:

When your activity is stopped, the Activity object is kept resident in memory and is recalled when the activity resumes. You don’t need to re-initialize components that were created during any of the callback methods leading up to the Resumed state.

and after:

It's uncommon that an app needs to use onRestart() to restore the activity's state, so there aren't any guidelines for this method that apply to the general population of apps. However, because your onStop() method should essentially clean up all your activity's resources, you'll need to re-instantiate them when the activity restarts. Yet, you also need to instantiate them when your activity is created for the first time (when there's no existing instance of the activity). For this reason, you should usually use the onStart() callback method as the counterpart to the onStop() method, because the system calls onStart() both when it creates your activity and when it restarts the activity from the stopped state.

Following what it is said in the first paragraph if I make an instance of something in onCreate() and onStart() methods I don't need to re-initialize them and ok, here it is clear, but if I make a transition from Stopped state to Resumed state then I have to come across the onStart() method, but if I made here an instane of something then it is re-made again! then what should I do?

zer0uno
  • 7,521
  • 13
  • 57
  • 86

1 Answers1

0

Since you don't want the instance to be re-initialized during onStart(), just initialize during onCreate() and use this if you want to save state simply from resuming again. However, to save more persistent data you must use something like SharedPreferences, a SQLite Database, or a file.

Community
  • 1
  • 1
jkau
  • 465
  • 4
  • 12