1

I tested my app in different phones. There is a strange thing. In all phones, the onCreate() never runs when I send my app to background using home button and come back to it. But in Nexus, the onCreate() method runs. The whole of my view is recreated which I want to avoid. I don't know how to save the views and other info in savedInstanceState. How to avoid rerunning onCreate()? Any help will be appreciated.

Edit: Even in activity life cycle, onResume() only is supposed to be executed after coming back to the activity from background.

enter image description here

azizbekian
  • 60,783
  • 13
  • 169
  • 249
berserk
  • 2,690
  • 3
  • 32
  • 63
  • 1
    Make sure you checked onResume(), the only thing that can reRun the onCreate() is that your app is exited somehow! – Saqib Dec 27 '13 at 06:53
  • @Saqib No my app is not exited. It is a mp3 player and keeps playing even in background. As mentioned in the life cycle, maybe my activity is destroyed by the system. – berserk Dec 27 '13 at 06:55
  • Then your activity must be destroyed by some other activity or process. As you can see that in lifecycle process your activity might have been killed by some other more priority process. Are you using a service for player? – Saqib Dec 27 '13 at 07:23
  • @Saqib No I am using async task in an activity – berserk Dec 27 '13 at 08:19
  • @Saqib I know I must use service to play, but it makes updating the player view in my app more difficult. I guess I must use broadcaster too – berserk Dec 27 '13 at 08:24

1 Answers1

1

But in Nexus, the onCreate method runs. The whole of my view is recreated which I want to avoid.

You shouldn't make assumptions on whether onCreate() will be called or no. Prepare your app for a situation, when system destroys app's process, which would initiate onCreate() to be called upon opening app. This can be easily tested with "Do not keep activities" developer mode.

I don't know how to save the views and other info in savedInstanceState.

If you use framework views (e.g. Button, TextView) which have id associated with them (e.g. android:id="@+id/some_id"), system will take care of saving their state, you should not care about it. If you have custom components you should take care of implementing that logics yourself.

How to avoid rerunning onCreate?

If system performed onCreate, that means it had good reason to do that. In other words, you cannot tell "please, do not call onCreate, jump to onResume right away".

Community
  • 1
  • 1
azizbekian
  • 60,783
  • 13
  • 169
  • 249