动作条向上导航再现父活动,而不是onResume [英] ActionBar up navigation recreates parent activity instead of onResume

查看:147
本文介绍了动作条向上导航再现父活动,而不是onResume的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的向上导航和我的code相像这样的:

  @覆盖
公共布尔onOptionsItemSelected(菜单项项){
    开关(item.getItemId()){
        案例android.R.id.home:
            意图H =新的意图(ShowDetailsActivity.this,MainActivity.class);
            h.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(H);
            返回true;
        默认值:返回super.onOptionsItemSelected(项目);
    }
}
 

下面是用例:

  1. 在我启动我的应用程序,这是MainActivity
  2. 在我点击一个按钮,进入ShowDetailsActivity
  3. 在我点击向上动作条导航

这个问题是在我点击UP,MainActivity它的onCreate()方法一遍击中,失去而不是在典型的onResume起()之类的那样,如果我只是从ShowDetailsActivity称为完成()的全部状态。为什么?请问这是怎么它始终工作,这有望为Android的行为重新被导航到使用向上的导航方法中,所有的活动?如果我打的后退按钮我得到预期的onResume生命周期。

这是我的解决方案,如果一个Android适当​​的人不存在:

  @覆盖
公共布尔onOptionsItemSelected(菜单项项){
    开关(item.getItemId()){
        案例android.R.id.home:
            意图upIntent =新的意图(这一点,MainActivity.class);
            如果(NavUtils.shouldU precreateTask(这一点,upIntent)){
                NavUtils.navigateUpTo(这一点,upIntent);
                完();
            } 其他 {
                完();
            }
            返回true;
        默认值:返回super.onOptionsItemSelected(项目);
    }
}
 

解决方案

以下内容添加到manifest文件中的父活动

 安卓launchMode =singleTop
 

关于本回答

I'm using the recommended approach for Up Navigation and my code looks like this:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent h = new Intent(ShowDetailsActivity.this, MainActivity.class);
            h.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(h);
            return true;
        default: return super.onOptionsItemSelected(item);
    }
}

Here's the use-case:

  1. I launch my app which is "MainActivity"
  2. I click a button to go to "ShowDetailsActivity"
  3. I click on the UP ActionBar navigation

The issue is after I click on UP, MainActivity hits its onCreate() methods all over again and loses all state instead of starting at the typical onResume() like it would if I just called "finish()" from ShowDetailsActivity. Why? Is this how it always works and this is expected behavior for Android to recreate all activities that are navigated to using the "Up" navigation approach? If I hit the back button I get the expected onResume lifecycle.

This is my solution if an Android "proper" ones doesn't exist:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent upIntent = new Intent(this, MainActivity.class);
            if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
                NavUtils.navigateUpTo(this, upIntent);
                finish();
            } else {
                finish();
            }
            return true;
        default: return super.onOptionsItemSelected(item);
    }
}

解决方案

Add the following to your parent activity in the manifest file

android:launchMode="singleTop"

regarding to this answer

这篇关于动作条向上导航再现父活动,而不是onResume的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆