简体   繁体   English

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

[英]Unable to handle back button in navigation fragment

In my app I am using a navigation drawer to go different fragment. 在我的应用程序中,我正在使用导航抽屉中的不同片段。 But when I clicked back button it exit from app. 但是,当我单击“后退”按钮时,它将从应用程序退出。 I want when back button clicked in any fragment it navigate to home fragment. 我想要在任何片段中单击后退按钮时导航到主页片段。

drawer_activity.java to navigate different fragment cabinet_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 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;
   }

In this fragment when one item click from recycler view it navigate to another fragment. 在此片段中,当一个项目从“回收者”视图中单击时,它将导航到另一个片段。 From that fragment when I click back button of my phone it exit from app. 当我单击手机的后退按钮时,该片段将从应用程序退出。 I want when back button clicked It goes home fragment always and only back button click from home fragment app exit. 我想要单击后退按钮时总是返回主页片段,并且仅从主页片段应用程序退出后退按钮单击。

productlistFragment.java 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 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>

Ok!! 好!! Just copy this code and paste you will get what yout want. 只需复制此代码并粘贴即可获得所需的内容。 ft.addToBackStack(null); is the key here. 是这里的关键。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Studio 打开导航抽屉使用片段和手柄后退按钮按下 - Android Studio open navigation Drawer using fragment and handle back button press 导航抽屉后退按钮到主要片段 - Navigation drawer back button to primary fragment 如何在导航栏中创建后退按钮的片段? - how to create back button in navigation bar in a fragment? 带有后退按钮的片段导航具有不良行为 - Fragment navigation with back button undesired behavior 在android中管理工具栏的导航和从片段到片段的后退按钮 - Manage toolbar's navigation and back button from fragment to fragment in android Android - 在处理带片段的导航时更新活动中的工具栏标题 - Android - update toolbar title in activity while handle back navigation with fragment 片段导航不适用于ActionBar上的“后退”按钮,对于物理按钮则不能 - Fragment navigation does not work with back button on ActionBar, ok with physical 无法使用导航情节提要板在片段中添加按钮以从一个片段跳转到另一个片段 - Unable to add a button in fragment using Navigation storyboard to jump from one fragment to another fragment 返回按钮到片段 - Back button to a fragment 导航到片段后无法通过菜单返回 go(带有显示问题的 gif) - Unable to go back through menu after navigation to fragment (with gif showing the problem)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM