如何使用导航抽屉从第二个片段返回上一个片段?

时间:2020-01-17 08:00:21

标签: android android-fragments

任何人都可以帮助我,如何使用导航抽屉从1st fragment返回2nd fragment 我已经尝试过fragmentTransaction.addToBackStack(null); 但它不起作用,它会发送到Home Fragment 这是代码 主页片段XML布局代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_home">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

第一个片段XML代码

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    android:id="@+id/select_product_video"
    android:background="@drawable/backgroundimage"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".activities.ui.products.SelectProductToUpdateFragment">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/contentHome_SwipeRefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:isScrollContainer="true">

            <LinearLayout
                android:id="@+id/allContent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recyclerViewAllProduct"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:padding="8dp"
                    android:scrollbars="vertical"
                    android:scrollingCache="true"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior">


                </androidx.recyclerview.widget.RecyclerView>

            </LinearLayout>

        </androidx.core.widget.NestedScrollView>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</FrameLayout>

第一个Fragment JAVA代码 OnClick的{​​{1}}方法

Adapter

ProductUpdateFragment Java代码

Fragment fragment = new ProductUpdateFragment();

FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.nav_host_fragment, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

活动代码

public class ProductUpdateFragment  extends Fragment  implements IOBackPressed {

    private SendViewModel sendViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        sendViewModel =
                ViewModelProviders.of(this).get(SendViewModel.class);
        View root = inflater.inflate(R.layout.fragment_send, container, false);
        final TextView textView = root.findViewById(R.id.text_send);
        sendViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
        return root;
    }

    @Override
    public boolean onBackPressed() {
        getActivity().onBackPressed();
        return false;
    }
}

2 个答案:

答案 0 :(得分:2)

通过tag传递BackStack

fragmentTransaction.replace(R.id.nav_host_fragment, "ProductUpdateFragment");
fragmentTransaction.addToBackStack("ProductUpdateFragment");

然后您可以执行BackStack()这样的

View root = inflater.inflate(R.layout.fragment_send, container, false);

root .setOnKeyListener( new View.OnKeyListener()
        {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {
                if(keyCode == KeyEvent.KEYCODE_BACK)
                {
                    getActivity().onBackPressed();
                    return true;
                }
                return false;
            }
        } );

OverrideActivity上实现此方法的地方,NavigationDrawer

    @Override
    public void onBackPressed()
    {
        //super.onBackPressed(); 
         int count = getFragmentManager().getBackStackEntryCount();
         if(count>0)
         getFragmentManager().popBackStack();
    }

答案 1 :(得分:0)

NavigationUI.navigateUp(Navigation.findNavController(this,R.id.nav_host_fragment),dataBinding.drawerLayout);