Reputation: 6522
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?
Upvotes: 27
Views: 34002
Reputation: 4341
You can add the flag Intent.FLAG_ACTIVITY_NO_ANIMATION
(link) to your reOpen
intent to eleminate all animations. But as stated from the other answer better refresh the data inside you Activity
.
Upvotes: 2
Reputation: 6622
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);
Upvotes: 49
Reputation: 21657
do you have some views that may change their value/size based on values from your shared preferences? if yes, create a method that init the views and call that methon on onCreate() method and in onClick() method.
Upvotes: 1