Bee*_*nny 6 android android-intent android-activity
活动A启动活动B.在活动B中,我有这个方法:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpTo(this, new Intent(this,
ArticleListActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
Run Code Online (Sandbox Code Playgroud)
当我按下那个主页按钮时,它会将我带到活动A.但是,onCreate再次被调用.我不想要这种行为.
我猜它是因为这个实现使用新的Intent到导航堆栈中的前一项.这只是我在创建双窗格项目时从Eclipse获得的代码.我查看了堆栈溢出,虽然似乎使用Intent返回导致此行为,但我不明白为什么Google会在默认模板中提供此功能.
我应该如何以不同的方式进行此调用,以便在返回活动A时不再调用onCreate?
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Run Code Online (Sandbox Code Playgroud)
如果您以前的活动是活动A,那么只需使用finish(); 方法而不是创建意图对象
正如@Rajesh 所指出的,您需要调用onBackPressed()。
您的代码检测到按下主页按钮并创建一个新的ArticleListActivity类。但是,您不想创建一个新类,您只想返回到已创建的类/活动。所以使用onBackPressed()代替。
| 归档时间: |
|
| 查看次数: |
5965 次 |
| 最近记录: |