Android的后退导航ParentActivityName不工作

问题描述:

我尝试使用上如何提供后退导航本规范Android的后退导航ParentActivityName不工作

https://developer.android.com/training/implementing-navigation/temporal.html

<activity android:name=".controller.general.AccountPickerScreen" 
       android:parentActivityName="ph.edu.upm.agila.extendthelife.controller.general.LogInScreen"> 
     <intent-filter> 
      <action android:name="ph.edu.upm.agila.extendthelife.controller.general.AccountPickerScreen" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 

我已经specificied的parentActivityName本教程,但每当我按下后退按钮应用退出?任何解决方案

试过这种

public void onBackPressed() { 
    Intent intent = NavUtils.getParentActivityIntent(AccountPickerScreen.this); 
    startActivity(intent); 
    finish(); 
} 

它的工作,但是我想知道,如果它是这样做的正确方法吗? 什么呢光洁度()做的正是

+0

完会破坏当前的活动.. –

+0

^有什么错我做了什么?我习惯于isuPatches –

+0

提供的解决方案是尝试删除到finish(),因为NavUtils.getParentActivityIntent也会执行finish()意味着finish()将被执行两次并可能关闭AccountPickscreen,并且不需要startActivity(intent)。 NavUtils.getParentActivityIntent将为你做所有的工作 –

您可能需要重写onBack的活动,并使用NavUtils来完成你想要什么有这样一行:

Intent intent = NavUtils.getParentActivityIntent(AccountPickerScreen.this); 
+0

你可以举个例子吗? –

+0

从你更新的例子和下面的评论它似乎你想出来:) – isuPatches

+0

谢谢btw :) –

的API级别低于16试着加入

<meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="fully qualified path to Activity Class Name" /> 
在活动标签

在AndroidManifest.xml

<activity android:name=".controller.general.AccountPickerScreen"> 
<intent-filter> 
    <action android:name="ph.edu.upm.agila.extendthelife.controller.general.AccountPickerScreen" /> 

    <category android:name="android.intent.category.DEFAULT" /> 
    <meta-data 
     android:name="android.support.PARENT_ACTIVITY" 
     android:value="ph.edu.upm.agila.extendthelife.controller.general.LogInScreen" /> 
</intent-filter> 
</activity> 

Ø verride onBackPressed with'

NavUtils.navigateUpFromSameTask(this);` 

我有一个问题,其中父按钮在一些活动上工作,但没有其他活动。

问题是我的onOptionsItemSelected函数。它默认返回true,阻止了呼叫完成。你应该调用super.onOptionsItemSelected(item)。

https://developer.android.com/guide/topics/ui/menus.html