【问题标题】:The official Google code for up button act weird向上按钮的官方 Google 代码行为怪异
【发布时间】:2013-05-15 13:16:27
【问题描述】:

我根据此处的 Google 建议实施了向上按钮: http://developer.android.com/training/implementing-navigation/ancestral.html

像这样:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // This is called when the Home (Up) button is pressed
        // in the Action Bar.
    case android.R.id.home:
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(
                Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

这段代码的最大问题是 MainActivity 是从一个新实例启动的。我想打开以前的 MainActivity 实例而不重新启动它。

由于这个活动非常繁重,我不想每次用户按下向上按钮时都创建一个新活动。

我尝试删除标志 FLAG_ACTIVITY_NEW_TASK 并使用所有可以想象的标志进行了很多操作,但我没有成功创建一个可以将 MainACtivity 恢复到以前状态的按钮。

任何帮助将不胜感激。

【问题讨论】:

    标签: android up-navigation


    【解决方案1】:

    你要找的是国旗:

    Intent.FLAG_ACTIVITY_REORDER_TO_FRONT

    哪个会做你想做的事——如果activity已经存在于task和stack中,那么它会被带到最前面,否则它会被重新创建。

    请记住,如果 Activity 已被 GC 销毁,那么无论如何都会重新创建它,并且某些 UI 组件可能会根据您获得初始化代码的位置而被重置(我不认为 @987654321 @ 被调用,但onResume() 肯定会被调用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-25
      • 2020-07-16
      • 2012-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多