Reload activity in Android

Clash Royale CLAN TAG#URR8PPP
Reload activity in Android
Is it a good practice to reload an Activity in Android?
Activity
What would be the best way to do it? this.finish and then this.startActivity with the activity Intent?
this.finish
this.startActivity
Intent
@Hudson It's what happens on configuration changes. If that is good design or not, it's another discussion entirely. ;)
– hpique
Jun 25 '13 at 12:30
14 Answers
14
You can Simply use
finish();
startActivity(getIntent());
to refresh an Activity from within itself.
Activity
What if it can't be called inside the activity class?, any way of doing it with the context reference?
– giorgiline
Nov 19 '12 at 9:58
@giorgiline - Yes, create an interface that you define inside the activity class, and pass that interface as a parameter to whatever external class you're using it from.
– aggregate1166877
Jun 11 '13 at 11:40
Wow, I did NOT think it would be this easy.
– theblang
Dec 9 '13 at 15:52
How can you deal with an activity which has been started for result? Can you restart that with the original caller still recieving a result?
– tothphu
Oct 7 '14 at 8:29
@tothphu - Not tried though, but theoretically I think yes. However the second time the activity gets finished, it won't return the result to the Activity created it for the first time.
– Sush
Oct 9 '14 at 11:33
This is what I do to reload the activity after changing returning from a preference change.
@Override
protected void onResume()
super.onResume();
this.onCreate(null);
This essentially causes the activity to redraw itself.
Updated: A better way to do this is to call the recreate() method. This will cause the activity to be recreated.
recreate()
Thanks ... Works like charm !!!
– user1744952
Nov 28 '13 at 13:32
That might be working but it's strongly not recommended to call lifecycle methods explicitly.
– ITisha
Feb 25 '14 at 16:49
work like a charm
– Hardik Gajera
Nov 4 '14 at 9:11
Great solution, except won't work in some situations when your lifecycle methods are used for other things. For instance, I am accessing the phone's camera, and when the photo is taken, the screen returns to my activity to post it into a view. By using lifecycle methods to refresh, I get kicked out of the camera process, and my photo does not return to the activity, but rather I get the activity from the start. Just an example to be aware of.
– Azurespot
Mar 31 '15 at 4:20
It's important to consider the launchMode of the activity when using recreate(). For example, when I call recreate() on an activity with launchMode=singleTop, I do not see onCreate() being called afterwards. OTOH, if, instead of calling recreate(), I explicitly call finish() and followed by startActivity(), I do see onCreate() being called afterwards.
– albert c braun
Nov 5 '16 at 15:52
for those who don't want to see that blink after recreate() method simply use
finish();
overridePendingTransition(0, 0);
startActivity(getIntent());
overridePendingTransition(0, 0);
Awesome! I wanted to override the Transition so that the user won't notice!
– Tuna
Aug 18 '17 at 13:03
I needed to update a message list in one of my applications in a hurry, so I just performed a refresh of my main UI activity before I closed the dialog I was in. I'm sure there are better ways to accomplish this as well.
// Refresh main activity upon close of dialog box
Intent refresh = new Intent(this, clsMainUIActivity.class);
startActivity(refresh);
this.finish(); //
simple and effeicient
– Matthew
Aug 8 '12 at 18:28
Android includes a process management system which handles the creation and destruction of activities which largely negates any benefit you'd see from manually restarting an activity. You can see more information about it at Application Fundamentals
What is good practice though is to ensure that your onPause and onStop methods release any resources which you don't need to hold on to and use onLowMemory to reduce your activities needs to the absolute minimum.
+1 for mentioning onLowMemory. I didn't know that such method existed!
– hpique
Jul 1 '10 at 10:38
Start with an intent your same activity and close the activity.
activity
activity
Intent refresh = new Intent(this, Main.class);
startActivity(refresh);//Start the same Activity
finish(); //finish Activity.
i have same problem
import android.support.v4.content.IntentCompat;
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
this code work for me .
Android api 17
in some cases it's the best practice in other it's not a good idea it's context driven
if you chose to do so using the following is the best way to pass from an activity to her sons :
Intent i = new Intent(myCurrentActivityName.this,activityIWishToRun.class);
startActivityForResult(i, GlobalDataStore.STATIC_INTEGER_VALUE);
the thing is whenever you finish() from activityIWishToRun you return to your a living activity
This is maybe old but you just saved me a lot of time! :)
– Lior Iluz
Feb 14 '11 at 22:22
I saw earlier answers which have been given for reloading the activity using Intent. Those will work but you can also do the same using recreate() method given in Activity class itself.
Instead of writing this
// Refresh main activity upon close of dialog box
Intent refresh = new Intent(this, clsMainUIActivity.class);
startActivity(refresh);
this.finish();
This can be done by writing this only
recreate();
I don't think that's a good idea... it'd be better to implement a cleaner method. For instance, if your activity holds a form, the cleaner method could just clear each widget in the form and delete all temporary data. I guess that's what you want: restore the activity to its initial state.
Actually, I want to change the application state and then reload the activity to reflect the change.
– hpique
Jun 16 '10 at 15:29
What is the "application state" in your case? Preferences?
– zehrer
Jun 18 '10 at 5:57
Yes, preferences.
– hpique
Jun 18 '10 at 14:23
After experimenting with this for a while I've found no unexpected consequences of restarting an activity. Also, I believe this is very similar to what Android does by default when the orientation changes, so I don't see a reason not to do it in a similar circumstance.
On the contrary, I tried to "refresh" my screen by restarting the activity. Itdoesn't appear on the surface anything went wrong, my information updated the how i wanted, the LogCat tells a different story. In LogCat, I go back into the onCreate method of the activity, it attempts to pull preferences. 1st attempt I receive a null pointer exception, then it attempts again to start in the onCreate and receives the storedPref the 2nd time through. Not sure what is going on here, but I just wanted to be sure that you weren't simply looking at the outcome, for WYSIWIG was not the case for me.
– taraloca
Sep 15 '10 at 19:38
How exactly are you restarting the activity?
– hpique
Sep 15 '10 at 20:57
I had another approach like: setting the launchMode of my activity to singleTop and without calling finish(), just startActivity(getIntent()) will do the job. Just have to care about the new data both in onCreate() and onNewIntent. With Sush's way, the application may blink like AMAN SINGH said. But AMAN SINGH's approach will still create a new activity which is somehow, unnecessary, even if he fixed the 'blink' problem, I think.
launchMode
singleTop
finish()
startActivity(getIntent())
onCreate()
onNewIntent
Too late for this question, but if someone looking for a solution, here it is.
simply use
this.recreate();
this will trigger the onCreate method in the activity
In an activity you can call recreate() to "recreate" the activity (API 11+)
recreate()
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
I am not an experienced Android developer, but restarting an Activity sounds like poor design and should only be done in extreme cases.
– Hudson
Jun 22 '13 at 20:25