hpique
hpique

Reputation: 120324

Reload activity in Android

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?

Upvotes: 272

Views: 571805

Answers (20)

ΓDΛ
ΓDΛ

Reputation: 11060

For kotlin extension

inline fun Activity.restart(intentBuilder: Intent.() -> Unit = {}) {
    val i = Intent(this, this::class.java)

Upvotes: 0

Md. Al-Amin
Md. Al-Amin

Reputation: 858

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

Upvotes: 4

gone
gone

Reputation: 823

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.

Upvotes: 1

Anil Kumar
Anil Kumar

Reputation: 1984

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

Upvotes: 3

Hocine Kheddadji
Hocine Kheddadji

Reputation: 63

i used this and it works fine without finish()

startActivity(getIntent());

Upvotes: 4

Rajnish Sharma
Rajnish Sharma

Reputation: 388

After login I had the same problem so I used

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

Upvotes: 1

itsam
itsam

Reputation: 301

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.

Upvotes: 6

kingargyle
kingargyle

Reputation: 1239

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.

Upvotes: 44

user10180461
user10180461

Reputation:

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

Upvotes: 3

Yasiru Nayanajith
Yasiru Nayanajith

Reputation: 1737

simply use

this.recreate();

this will trigger the onCreate method in the activity

Upvotes: 41

Rizwan
Rizwan

Reputation: 1664

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

Upvotes: 23

yoav.str
yoav.str

Reputation: 1542

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

Upvotes: 4

AMAN SINGH
AMAN SINGH

Reputation: 3561

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

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

Upvotes: 116

Sush
Sush

Reputation: 6899

You can Simply use

finish();
startActivity(getIntent());

to refresh an Activity from within itself.

Upvotes: 634

Adnan Abdollah Zaki
Adnan Abdollah Zaki

Reputation: 4406

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

Upvotes: 3

Jorgesys
Jorgesys

Reputation: 126455

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.

Upvotes: 6

ninehundreds
ninehundreds

Reputation: 1105

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

Upvotes: 13

Al Sutton
Al Sutton

Reputation: 3924

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.

Upvotes: 11

hpique
hpique

Reputation: 120324

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.

Upvotes: 3

Cristian
Cristian

Reputation: 200080

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.

Upvotes: 3

Related Questions