Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh activity without re-opening it?

Tags:

I implemented a button in my app that clears all sharedpreferences using this code:

context.getSharedPreferences("bifrostPrefs", 0).edit().clear().commit(); 

Now the problem is that whenever I use the button, I then need to exit the activity and re-open it to see results. I tried solving this by simply making the button re-open the activity with this code:

Intent reOpen = new Intent (Bifrost.this, Bifrost.class); startActivity(reOpen); 

My idea seemed smart until I noticed that if I re-open the activity, I then need to press the back button twice to get back to main activity. So I did some reserach and found this code:

finish(); startActivity(getIntent()); 

This now works fine, the activity gets refreshed and then I only need to click the back button once. But is there another way to refresh activity without it "flashing" in and out? As you know, everytime you open a new activity, it flashes in and out so the app lags for a second. Is there a way to refresh an activity by bypassing this?

like image 554
Guy Avatar asked Jul 05 '13 12:07

Guy


1 Answers

Well, it would be better to update the content of the activity, but if it's too complicated you can override the default animation with this method :

finish(); overridePendingTransition( 0, 0); startActivity(getIntent()); overridePendingTransition( 0, 0); 
like image 82
Stephane Mathis Avatar answered Oct 06 '22 04:10

Stephane Mathis