I have two activities and one fragment in my app. The first activity, MainActivity
, is used to hold the fragment. It contains an ActionBarSherlock
ActionBar
and the fragment, WFrag
. WFrag
opens up the second activity, SettingsActivity
, when you press a button. In SettingsActivity
, you can choose settings that affect the behavior of WFrag
. Naturally, I want the onBackPressed()
method to go back to WFrag
and update it with the user's new settings. My question is: how do you make it so that when you go back (via the back button) from SettingsActivity
, you recreate (call onCreate()
again) WFrag
?
问问题
1693 次
2 回答
1
- 覆盖 onBackPressed 或
- 在 onStart/onResume 中添加更新
于 2013-08-16T00:27:21.880 回答
1
首先你必须重写 SettingActivity 的 onBackPressed()。
其次,当您开始第二个活动时,即 MainActivity 中的 SettingActivity,您必须以这种方式清除 Stack of previous activity
Intent intent = new Intent(this, SettingActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
onBackPressed() 方法中的 SettingActivity 也是如此,您必须以这种方式运行 MainActivity
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
或者,如果您从按钮单击重定向到 MainActivity,您可以使用上面的代码。
于 2016-04-08T05:19:42.287 回答