You can Simply use

finish();

startActivity(getIntent());

to refresh an Activity from within itself.

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.

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

finish();

overridePendingTransition(0, 0);

startActivity(getIntent());

overridePendingTransition(0, 0);

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

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.

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

simply use

this.recreate();

this will trigger the onCreate method in the activity

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.

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

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

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.

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.

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

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.

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.

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

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐