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
Starting an activity You can start a new instance of an Activity by passing an Intent to startActivity() . The Intent describes the activity to start and carries any necessary data. If you want to receive a result from the activity when it finishes, call startActivityForResult() .
For recreating the activity, you can use the simple recreate() or startActivity(intent). or can use to reopen the activty by startActivity.
Restore Your Activity State When your activity is recreated after it was previously destroyed, you can recover your saved state from the Bundle that the system passes your activity. Both the onCreate() and onRestoreInstanceState() callback methods receive the same Bundle that contains the instance state information.
This example demonstrates how to restart an Android Activity using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With