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
?
You can Simply use
finish();
startActivity(getIntent());
to refresh an Activity
from within itself.
for those who don't want to see that blink after recreate() method simply use
finish();
overridePendingTransition(0, 0);
startActivity(getIntent());
overridePendingTransition(0, 0);
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.
simply use
this.recreate();
this will trigger the onCreate method in the activity
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 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.
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.
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.
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 used this and it works fine without
finish()
startActivity(getIntent());
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.
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 an activity you can call recreate()
to "recreate" the activity (API 11+)
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);
After login I had the same problem so I used
@Override
protected void onRestart() {
this.recreate();
super.onRestart();
}
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.
精彩评论