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
?
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
?
Start with an intent your same
activity
and close theactivity
.for those who don't want to see that blink after recreate() method simply use
This is what I do to reload the activity after changing returning from a preference change.
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 will trigger the onCreate method in the activity
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 had another approach like: setting the
launchMode
of my activity tosingleTop
and without callingfinish()
, juststartActivity(getIntent())
will do the job. Just have to care about the new data both inonCreate()
andonNewIntent
. 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.