269

Is it a good practice to reload an Activity in Android?

What would be the best way to do it? this.finish and then this.startActivity with the activity Intent?

N J
  • 27,217
  • 13
  • 76
  • 96
hpique
  • 119,096
  • 131
  • 338
  • 476

19 Answers19

628

You can Simply use

finish();
startActivity(getIntent());

to refresh an Activity from within itself.

N J
  • 27,217
  • 13
  • 76
  • 96
Sush
  • 6,839
  • 1
  • 18
  • 26
  • 3
    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 09:58
  • 1
    @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
  • 3
    Wow, I did NOT think it would be this easy. – theblang Dec 09 '13 at 15:52
  • 1
    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 07 '14 at 08:29
  • 1
    @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 09 '14 at 11:33
  • And from fragment: `getActivity().finish(); startActivity(getActivity().getIntent());` – Sava Dimitrijević Aug 06 '18 at 22:42
  • 2
    the activity will be invalid after finish() is invoke, im just curious why this work. can u answer me? – Lorence Hernandez Aug 25 '18 at 13:33
110

for those who don't want to see that blink after recreate() method simply use

 finish();
 overridePendingTransition(0, 0);
 startActivity(getIntent());
 overridePendingTransition(0, 0);
AMAN SINGH
  • 3,491
  • 6
  • 28
  • 44
44

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.

ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253
kingargyle
  • 1,239
  • 10
  • 15
  • 19
    That might be working but it's strongly not recommended to call lifecycle methods explicitly. – ITisha Feb 25 '14 at 16:49
  • 1
    If you take this route, you should use a separate init() method which sets everything up and call that from within onCreate() and onResume(), instead of calling onCreate(). – Tom Oct 09 '14 at 22:42
  • 2
    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 04:20
  • 2
    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 05 '16 at 15:52
38

simply use

this.recreate();

this will trigger the onCreate method in the activity

Yasiru Nayanajith
  • 1,647
  • 17
  • 20
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();
Rizwan
  • 1,461
  • 12
  • 26
  • `recreate()` was the solution for me - "restarting" the activity didn't work for me, as I started the activity using "StartActivityForResult" and it broke the behaviour. – KYL3R Dec 29 '19 at 22:58
12

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(); //
Irgendw Pointer
  • 1,770
  • 3
  • 30
  • 67
ninehundreds
  • 1,097
  • 2
  • 21
  • 43
11

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.

Yaroslav
  • 6,476
  • 10
  • 48
  • 89
Al Sutton
  • 3,904
  • 1
  • 22
  • 17
6

Start with an intent your same activity and close the activity.

Intent refresh = new Intent(this, Main.class);
startActivity(refresh);//Start the same Activity
finish(); //finish Activity.
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
6

Reloading your whole activity may be a heavy task. Just put the part of code that has to be refreshed in (kotlin):

override fun onResume() {
    super.onResume()
    //here...
}

Java:

@Override
public void onResume(){
    super.onResume();
    //here...

}

And call "onResume()" whenever needed.

itsam
  • 301
  • 2
  • 10
4

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

Ambrish Pathak
  • 3,813
  • 2
  • 15
  • 30
yoav.str
  • 1,547
  • 6
  • 33
  • 73
4

i used this and it works fine without finish()

startActivity(getIntent());
4

To refresh an activity, you can call:

this.recreate();

Or, for any CustomAdapters:

context.recreate(); 

Try this or,

((Activity) context).recreate();

Or, for any Fragments:

getActivity().recreate();

You can call this from anywhere you want to refresh your activity. If you want to refresh an activity coming back from another activity, you can call this onRestart() function.

@Override
    protected void onRestart() {
        this.recreate();  // refresh activity
        super.onRestart();
    }
Md. Al-Amin
  • 690
  • 3
  • 13
3

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

Adnan Abdollah Zaki
  • 4,328
  • 6
  • 52
  • 58
3

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.

Cristian
  • 198,401
  • 62
  • 356
  • 264
3

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.

hpique
  • 119,096
  • 131
  • 338
  • 476
  • 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
3

In an activity you can call recreate() to "recreate" the activity (API 11+)

3

for me it's working it's not creating another Intents and on same the Intents new data loaded.

    overridePendingTransition(0, 0);
    finish();
    overridePendingTransition(0, 0);
    startActivity(getIntent());
    overridePendingTransition(0, 0);
Anil Kumar
  • 1,830
  • 15
  • 24
1

I had another approach like: setting the launchMode of my activity to singleTop and without calling finish(), just startActivity(getIntent()) will do the job. You just have to care about the new data in onCreate() and onNewIntent. With Sush's way, the application may blink as 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.

Too late for this question, but if someone looking for a solution, here it is.

gone
  • 823
  • 8
  • 21
1

After login I had the same problem so I used

@Override
protected void onRestart() {
    this.recreate();
    super.onRestart();
}
Rajnish Sharma
  • 390
  • 2
  • 14