views:

522

answers:

1

My app intiates an activity. On the click of a button, the app opens up the browser with a webpage. When I hit the back button, it comes back to my initial activity screen, but does not resume or restart the activity.

When I put all the layout code and activity code in onResume instead of onCreate, the activity gets restarted.

My question is whether this is the right way to go about it? Can I use onResume to draw my layout and initiate the activity, or is this poor design? When the browser fires up, does the initial activity forget its layout?

Please let me know what you suggest.

Thanks Chris

+1  A: 

Mostly you should read about the Activity Life Cycle.

It is fine to initialize in onResume as long as you only do it once. Either have a dedicated hasInitialized member or check some other value that will have equivalent meaning, and do not initialize again if it is set.

drawnonward
My question is more around the fact that having the layout creation and activity starting in the onCreate method started up things alright, but when I came back from the browser, only the layout was rendered, but the activity was not started. What exactly happens to the layout when the browser opens up? Putting the same code in onResume, starts up everything and restarts activity after back button press.
Chris
When I drew the layout in onCreate and started the activity, on the back button press from the browser, the layout is rendered but the activity does not restart.When I did nothing in onCreate, but drew the layout in onResume and started my activity, the back button press rendered the layout and restarted the activity.
Chris
I agree with the answer: you should read the linked-to page: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle "but when I came back from the browser, only the layout was rendered, but the activity was not started" -- no, the activity was most definitely started, as defined by a call to `onStart()`.
CommonsWare
As you can see in the linked graphic, onResume can be called after onStart or onPause, and onStart can be called after onCreate or onRestart.
drawnonward
Thanks all. Inspite of re-reading the life cycle so many times, I wasn't exactly sure which methods my app was calling. I tried putting Log.v in all the methods and watched it run, and now things are crystal clear! Thanks everyone!
Chris