Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between recreating an Activity with recreate() method and startActivity(getIntent())

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

like image 731
A Nice Guy Avatar asked Jan 23 '14 17:01

A Nice Guy


People also ask

What does startActivity do?

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() .

How do you recreate an activity?

For recreating the activity, you can use the simple recreate() or startActivity(intent). or can use to reopen the activty by startActivity.

When activity is recreated?

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.

How do you restart an activity on Kotlin?

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.


1 Answers

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

like image 148
Abs Avatar answered Sep 29 '22 05:09

Abs