17

I am more or less a beginner in android programming

My Question follows from this post.

As far as I can gather, there are mainly two ways to restart the same Activity I am in:

a)Activity.recreate() [ added after API 11 ]

b)

Intent intent = getIntent();
    finish();
    startActivity(intent);

How does these two actually work? Are there any difference in the process they recreate the activity?

I believe there must be some difference between the way these two recreates the activity, because, I have seen that recreate() adds some default(junk?) values to the views in my activity. Also, recreate() starts the new activity with a default black splash view

Community
  • 1
  • 1
A Nice Guy
  • 2,676
  • 4
  • 30
  • 54

1 Answers1

15

Recreate - (You can restore state of activity) This results in essentially the same flow as when the Activity is created due to a configuration change -- the current instance will go through its lifecycle to onDestroy() and a new instance then created after it. It also means ViewModel is not destroyed.

The recreate() method acts just like a configuration change, so your onSaveInstanceState() and onRestoreInstanceState() methods are also called, if applicable.

Very interesting read: http://developer.android.com/training/basics/activity-lifecycle/recreating.html

vs

Finish The ActivityResult is propagated back to whoever launched you via onActivityResult(). and Started again as new activity on top of the stack

Jemshit
  • 9,501
  • 5
  • 69
  • 106
Abs
  • 3,902
  • 1
  • 31
  • 30
  • This is wrong. `onSaveInstanceState` is **NOT** called. It's only called if the system kills the activity. – Martin Konecny Sep 15 '15 at 19:04
  • 3
    @MartinKonecny - The docs are very specific that calling `recreate()` has the same effect as a configuration change: _"the current instance will go through its lifecycle to onDestroy() and a new instance then created after it."_. When that happens, `onSaveInstanceState()` **is** called, just as it is when the configuration changes (unless you've changed the default behavior via manifest flags). – Ted Hopp Oct 11 '16 at 07:18
  • thanks, vm not destroyed statement was helpful – shubham chouhan Feb 07 '22 at 06:07