Reload activity in Android

AndroidAndroid Activity

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

You can Simply use

finish();
startActivity(getIntent());

to refresh an Activity from within itself.

Solution 2 - Android

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

 finish();
 overridePendingTransition(0, 0);
 startActivity(getIntent());
 overridePendingTransition(0, 0);

Solution 3 - Android

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.

Solution 4 - Android

simply use

this.recreate();

this will trigger the onCreate method in the activity

Solution 5 - Android

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

Solution 6 - Android

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(); //

Solution 7 - Android

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.

Solution 8 - Android

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.

Solution 9 - Android

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.

Solution 10 - Android

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

Solution 11 - Android

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.

Solution 12 - Android

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.

Solution 13 - Android

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

Solution 14 - Android

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

Solution 15 - Android

i used this and it works fine without finish()

startActivity(getIntent());

Solution 16 - Android

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.

Solution 17 - Android

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);

Solution 18 - Android

After login I had the same problem so I used

@Override
protected void onRestart() {
    this.recreate();
    super.onRestart();
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhpiqueView Question on Stackoverflow
Solution 1 - AndroidSushView Answer on Stackoverflow
Solution 2 - AndroidAMAN SINGHView Answer on Stackoverflow
Solution 3 - AndroidkingargyleView Answer on Stackoverflow
Solution 4 - AndroidYasiru NayanajithView Answer on Stackoverflow
Solution 5 - AndroidRizwanView Answer on Stackoverflow
Solution 6 - AndroidninehundredsView Answer on Stackoverflow
Solution 7 - AndroidAl SuttonView Answer on Stackoverflow
Solution 8 - AndroidJorgesysView Answer on Stackoverflow
Solution 9 - AndroiditsamView Answer on Stackoverflow
Solution 10 - Androidyoav.strView Answer on Stackoverflow
Solution 11 - AndroidCristianView Answer on Stackoverflow
Solution 12 - AndroidhpiqueView Answer on Stackoverflow
Solution 13 - AndroidAdnan Abdollah ZakiView Answer on Stackoverflow
Solution 14 - Androiduser10180461View Answer on Stackoverflow
Solution 15 - AndroidHocine KheddadjiView Answer on Stackoverflow
Solution 16 - AndroidgoneView Answer on Stackoverflow
Solution 17 - AndroidAnil KumarView Answer on Stackoverflow
Solution 18 - AndroidRajnish SharmaView Answer on Stackoverflow