Jav*_*per 7 navigation android android-fragments
在创建一个非常简单的示例应用程序时,当我按下模拟器上的硬件后退按钮时,我无法理解为什么我的应用程序正在关闭。
我有 1 个 mainActivity 和 2 个片段。
当我在 NavigationFragment 上并按回时,应用程序将关闭而不是返回到 IntermediateFragment。
主要活动:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
toolbar.setTitle(R.string.app_name)
}
}
Run Code Online (Sandbox Code Playgroud)
活动_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.exampleapplication.MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_constraintTop_toTopOf="parent"
/>
<fragment
android:id="@+id/my_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/main_nav"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
导航图:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_nav"
app:startDestination="@+id/intermediateFragment">
<fragment
android:id="@+id/intermediateFragment"
android:name="com.exampleapplication.IntermediateFragment">
<action
android:id="@+id/action_intermediate_to_navigation"
app:destination="@+id/navigationFragment"
/>
</fragment>
<fragment
android:id="@+id/navigationFragment"
android:name="com.exampleapplication.NavigationFragment"
/>
</navigation>
Run Code Online (Sandbox Code Playgroud)
中间片段:
class IntermediateFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_intermediate, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
btn_next_fragment.setOnClickListener {
findNavController().navigate(R.id.action_intermediate_to_navigation)
}
}
}
Run Code Online (Sandbox Code Playgroud)
导航片段:
class NavigationFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_navigation, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
btn_first_library.setOnClickListener {
findNavController().setGraph(R.navigation.first_library_nav)
}
btn_download_pdf.setOnClickListener {
findNavController().setGraph(R.navigation.download_pdf_nav)
}
}
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
ian*_*ake 18
您的<fragment>:上缺少一行:
app:defaultNavHost="true"
Run Code Online (Sandbox Code Playgroud)
根据导航入门指南:
该
app:defaultNavHost="true"属性确保您NavHostFragment拦截系统的 Back 按钮。
由于您没有设置,导航不会拦截后退按钮,因此,您只能获得默认活动行为(即关闭您的活动)。
| 归档时间: |
|
| 查看次数: |
1569 次 |
| 最近记录: |