Android Jet Pack体系结构组件导航在Java中不起作用?

时间:2018-11-08 05:06:54

标签: android android-studio android-jetpack

我在Java中的android jet pack导航中遇到问题。使用这个,当我从一个片段转到另一个片段时,先前的片段被破坏了,但是我想保存先前的片段历史。在科特林,它工作正常。

例如,我有一个三个片段,其中每个片段都有一个编辑文本。当我在片段1中输入文本并转到下一个片段2时,片段1被破坏。当我从片段2返回时,片段1正在重新创建。使用此体系结构,片段暂停和恢复状态不起作用。

MainActivity.java

    public class MainActivity extends AppCompatActivity {

        FrameLayout sectionHomeWrapper;


        NavController navHomeController;
        Fragment navHomeFragment;


        NavController currentController = null;


        BottomNavigationView navigation = null;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            sectionHomeWrapper=findViewById(R.id.section_home_wrapper);


            navHomeController= Navigation.findNavController(this,R.id.section_home);

          navigation=(BottomNavigationView)findViewById(R.id.navigation);
            currentController = navHomeController;
            NavigationUI.setupWithNavController(navigation, currentController);


            sectionHomeWrapper.setVisibility(View.VISIBLE);


        }

    }

其中添加了navHost的主要活动布局文件 activity_main.xml

<android.support.constraint.ConstraintLayout
    android:id="@+id/container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/section_home_wrapper"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/navigation"
        app:layout_constraintTop_toTopOf="parent"
        >

        <fragment
            android:id="@+id/section_home"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="false"
            app:navGraph="@navigation/nav_graph_section_home"
            />
    </FrameLayout>



    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation"/>

</android.support.constraint.ConstraintLayout>
导航链接到的

导航图文件。 nav_graph_section_home.xml

<?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"
    app:startDestination="@id/navigation_home"
    >
    <fragment
        android:id="@+id/navigation_home"
        android:name="com.dps.navigation.HomeFragment"
        android:label="HomeFragment">

        <action
            android:id="@+id/action_notificationFragment_to_detailFragment"
            app:destination="@id/detailFragment"/>
    </fragment>

    <fragment
        android:id="@+id/homeDetailFragment"
        android:name="com.dps.navigation.DetailFragment"
        android:label="HomeDetailFragment">

    </fragment>

    <fragment
        android:id="@+id/navigation_dashboard"
        android:name="com.dps.navigation.DashboardFragment"
        android:label="DashboardFragment">
        <action
            android:id="@+id/action_notificationFragment_to_detailFragment"
            app:destination="@id/detailFragment"/>
    </fragment>

    <fragment
        android:id="@+id/navigation_notifications"
        android:name="com.dps.navigation.NotificationFragment"
        android:label="DashboardFragment">
        <action
            android:id="@+id/action_notificationFragment_to_detailFragment"
            app:destination="@id/detailFragment"/>
    </fragment>

    <fragment
        android:id="@+id/detailFragment"
        android:name="com.dps.navigation.DetailFragment"
        android:label="DetailFragment"/>
</navigation>

enter image description here

1 个答案:

答案 0 :(得分:0)

解决方案:

代替:

navController=Navigation.findNavController(view);
navController.navigate(R.id.next_action, args);

在您的按钮中写上:

Navigation.findNavController(view).navigate(R.id.your_action);

如果有任何问题,请发表评论。