无法处理导航片段中的后退按钮

时间:2018-10-06 10:30:16

标签: java android android-fragments

在我的应用中,我正在使用导航抽屉中的不同片段。但是,当我单击“后退”按钮时,它将从应用程序退出。我想在任何片段中单击“后退”按钮时,都导航到主页片段。

  

drawer_activity.java浏览不同的片段

public boolean onNavigationItemSelected(MenuItem item) {
    Fragment fragment = null;
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.home) {
        toolbar.setTitle("HOME");
        fragment = new HomeFragment();
    } else if (id == R.id.wishlist) {
        toolbar.setTitle("YOUR WISHLIST");
    } else if (id == R.id.order) {
        toolbar.setTitle("YOUR ORDER HISTORY");
    } else if (id == R.id.cart) {
        toolbar.setTitle("YOUR CART");
    } else if (id == R.id.nav_share) {
        toolbar.setTitle("HOME");
    } else if (id == R.id.account) {
        toolbar.setTitle("YOUR ACCOUNT");
    } else if (id == R.id.logout) {
        finish();
        SharedPrefManager.getInstance(getApplicationContext()).logout();
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();

        ft.replace(R.id.content, fragment);
        ft.commit();
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
   }
  

homefragment.java

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.activity_home, container,false);

    //getting the recyclerview from xml
    recyclerView = (RecyclerView) rootView.findViewById(R.id.recylcerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));

    categoryList = new ArrayList<>();


    loadCategory();
    customCategoryList = new CustomCategoryList(getActivity(),categoryList);
    recyclerView.setAdapter(customCategoryList);
    recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new RecyclerTouchListener.ClickListener() {
        @Override
        public void onClick(View view, int position) {
            int id = categoryList.get(position).getCategoryid();
            String category = categoryList.get(position).getCategoryname();
            final GlobalVariable ID = (GlobalVariable)getActivity().getApplication();
            ID.setCategoryid(id);
            ID.setCategory(category);
            Log.e("categoryid",ID.getCategoryid()+"");
            Fragment fragment = new ProductListFragment();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction ft = fragmentManager.beginTransaction();

            ft.replace(R.id.content, fragment);
            ft.commit();

        }

        @Override
        public void onLongClick(View view, int position) {

        }
    }));
    return rootView;
   }

在此片段中,当一个项目从回收者视图中单击时,它将导航到另一个片段。当我单击手机的后退按钮时,该片段将从应用程序退出。我想要单击后退按钮时始终返回首页片段,并且仅从首页片段应用程序退出中单击后退按钮。

  

productlistFragment.java

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.activity_product_list, container,false);

    gridView = (GridView) rootView.findViewById(R.id.productlist);
    category = (TextView) rootView.findViewById(R.id.category);
    final GlobalVariable ID = (GlobalVariable)getActivity().getApplication();
    categoryid = ID.getCategoryid();
    category.setText(ID.getCategory());

    productList = new ArrayList<>();
    loadProduct();

    customProductList = new CustomProductList(getActivity(),productList);
    gridView.setAdapter(customProductList);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            productid = productList.get(position).getProductid();
            final GlobalVariable Id = (GlobalVariable)getActivity().getApplication();
            Id.setProductid(productid);
            Intent intent = new Intent(getActivity(), ProductView.class);
            startActivity(intent);
        }
    });

    return rootView;
    }
  

content.xml

<android.support.constraint.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:context=".DrawerActivity"
tools:showIn="@layout/app_bar_drawer">

<FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>

</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:1)

好!!只需复制此代码并粘贴即可获得所需的内容。ft.addToBackStack(null);是这里的关键。

Fragment fragment = new ProductListFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.addToBackStack(null);
ft.replace(R.id.content, fragment);
ft.commit();