刷新活动而不重新打开吗? [英] Refresh activity without re-opening it?
问题描述
我在应用程序中实现了一个按钮,该按钮使用以下代码清除了所有共享首选项:
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?
推荐答案
好,最好更新活动的内容,但是如果太复杂,则可以使用以下方法覆盖默认动画:
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);
这篇关于刷新活动而不重新打开吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!