Android 从包含DialogFragment的片段向上导航将重新显示DialogFragment导航组件
我正在使用导航组件,我已经设置了向上箭头,以便在我唯一的活动Android 从包含DialogFragment的片段向上导航将重新显示DialogFragment导航组件,android,android-jetpack,android-architecture-navigation,android-jetpack-navigation,Android,Android Jetpack,Android Architecture Navigation,Android Jetpack Navigation,我正在使用导航组件,我已经设置了向上箭头,以便在我唯一的活动main活动中自动处理导航过程,我有: override fun onOptionsItemSelected(item: MenuItem): Boolean { return item.onNavDestinationSelected(navController) || super.onOptionsItemSelected(item) } 当用户从StationsFragment单击菜单项时,将显示该对话框,如下所示: o
main活动中自动处理导航过程,我有:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return item.onNavDestinationSelected(navController) || super.onOptionsItemSelected(item)
}
当用户从StationsFragment
单击菜单项时,将显示该对话框,如下所示:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val bundle = Bundle()
bundle.putInt(GAME_ID_BUNDLE_KEY, gameId)
findNavController().navigate(R.id.action_stationsFragment_to_gameInfoDialog, bundle)
return true
}
<fragment
android:id="@+id/stationsFragment"
android:name="com.accad.accadgame.screens.fragments.StationsFragment"
android:label="@string/stations_fragment_title"
tools:layout="@layout/fragment_stations"
>
<argument
android:name="game_id"
app:argType="integer"
android:defaultValue="-1" />
<action
android:id="@+id/action_stationsFragment_to_sectionsFragment"
app:destination="@id/sectionsFragment"
app:popUpTo="@+id/stationsFragment"
app:popUpToInclusive="false" />
<action
android:id="@+id/action_stationsFragment_to_gameInfoDialog"
app:destination="@id/gameInfoDialog"
app:popUpTo="@id/stationsFragment"
app:popUpToInclusive="false"
/>
</fragment>
<dialog
android:id="@+id/gameInfoDialog"
android:name="com.accad.accadgame.screens.dialogs.GameInfoDialog"
android:label="GameInfoDialog"
tools:layout="@layout/dialog_game_info"
>
<argument
android:name="game_id"
app:argType="integer"
android:defaultValue="-1" />
我已经将导航图设置为:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val bundle = Bundle()
bundle.putInt(GAME_ID_BUNDLE_KEY, gameId)
findNavController().navigate(R.id.action_stationsFragment_to_gameInfoDialog, bundle)
return true
}
<fragment
android:id="@+id/stationsFragment"
android:name="com.accad.accadgame.screens.fragments.StationsFragment"
android:label="@string/stations_fragment_title"
tools:layout="@layout/fragment_stations"
>
<argument
android:name="game_id"
app:argType="integer"
android:defaultValue="-1" />
<action
android:id="@+id/action_stationsFragment_to_sectionsFragment"
app:destination="@id/sectionsFragment"
app:popUpTo="@+id/stationsFragment"
app:popUpToInclusive="false" />
<action
android:id="@+id/action_stationsFragment_to_gameInfoDialog"
app:destination="@id/gameInfoDialog"
app:popUpTo="@id/stationsFragment"
app:popUpToInclusive="false"
/>
</fragment>
<dialog
android:id="@+id/gameInfoDialog"
android:name="com.accad.accadgame.screens.dialogs.GameInfoDialog"
android:label="GameInfoDialog"
tools:layout="@layout/dialog_game_info"
>
<argument
android:name="game_id"
app:argType="integer"
android:defaultValue="-1" />
当我点击信息菜单项时,对话框会正常显示
当我关闭对话框并单击站片段的向上箭头时,经过长时间搜索后,对话框再次显示,返回箭头也被视为一个菜单项
因此,当单击后退箭头时,将调用选项ItemSelected方法,需要检查菜单项id
代码为:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if(item.itemId == R.id.gameInfo) {
val bundle = Bundle()
bundle.putInt(GAME_ID_BUNDLE_KEY, gameId)
findNavController().navigate(R.id.action_stationsFragment_to_gameInfoDialog, bundle)
return true
}
return super.onOptionsItemSelected(item)
}