Reputation: 2725
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
Upvotes: 18
Views: 13846
Reputation: 3962
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
Upvotes: 15