Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按下后退按钮调用onCreate(Android)_Android_Android Intent_Android Activity - Fatal编程技术网

按下后退按钮调用onCreate(Android)

按下后退按钮调用onCreate(Android),android,android-intent,android-activity,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

活动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);
}
当我按下“主页”按钮时,它会带我进入活动A。但是,再次调用onCreate。我不想要这种行为

我猜这是因为此实现使用了导航堆栈中前一项的新意图。这只是我在创建双窗格项目时从Eclipse获得的代码。我环顾了一下堆栈溢出,虽然使用返回的意图似乎是导致这种行为的原因,但我不明白为什么谷歌会在默认模板中提供这种行为


我应该如何进行不同的调用,以便在返回活动A时不会再次调用onCreate?

正如@Rajesh所指出的,您需要调用onBackPressed()

您的代码检测到home按钮按下并创建一个新的ArticleListActivity类。但是,您不想创建一个新类,只想返回到您已经创建的类/活动。因此,请改用onBackPressed()

只需完成当前活动,即可完成上一个活动
如果您以前的活动是活动A,则只需使用finish();方法而不是创建意图对象,您可以尝试在清单中设置启动模式,因为父活动可以从堆栈中弹出

android:launchMode="singleTop"
这样,就不会再次调用onCreate()

@Override
public void onBackPressed() {
    super.onBackPressed();
    finish();
}

您可以只调用onBackPressed()只需完成此活动,即调用this.finish();谢谢你,伙计。作为一名iOS开发人员,我一直在寻找返回主表的简单无缝导航。这真是太棒了!
@Override
public void onBackPressed() {
    super.onBackPressed();
    finish();
}