后退按钮关闭应用程序,而不是转到上一个片段android导航组件

时间:2019-11-27 21:06:13

标签: android android-fragments navigation

在创建一个非常简单的示例应用程序时,当我按下模拟器上的硬件后退按钮时,我无法为为什么关闭我的应用程序而费解。

我有1个mainActivity和2个片段。

当我在NavigationFragment上并按回时,该应用程序将关闭,而不是返回到IntermediateFragment。

MainActivity:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        toolbar.setTitle(R.string.app_name)
    }
}

activity_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>

导航图:

<?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>

中间片段:

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)
        }
    }
}

NavigationFragment:

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)
        }
    }
}

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您在<fragment>上缺少一行:

app:defaultNavHost="true"

按照Navigation Getting Started guide

  

app:defaultNavHost="true"属性可确保您的NavHostFragment截获系统的“后退”按钮。

由于您未进行设置,因此导航不会拦截后退按钮,因此,您只会获得默认的活动行为(这将关闭您的活动)。