Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to set backpress Android back button not working in fragment

I am new to android,Fragmentbell to fragmenthome back not working enter image description here how to fix fragmentbell to fragmenthome private void goToHome() { not working this line backpress see this image any how to fix this erro not working any backpress

   public class Fragmentbell extends Fragment {


    LinearLayout linearLayout;
    private View root_view;
    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private Adapterbell adapterbell;
    public static final String EXTRA_OBJC = "key.EXTRA_OBJC";
    DbHandler databaseHandler;

    private ArrayList<bell> mNotificationList;

    private ShimmerFrameLayout lyt_shimmer;
    SharedPref sharedPref;
    private List<bell> data = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {

        root_view = inflater.inflate(R.layout.fragment_bell, container, false);


        //Call this method before you set you adapter
        //then set your adapter
        loadDataFromDatabase();

        recyclerView = root_view.findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);

        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, LinearLayoutManager.VERTICAL));
        recyclerView.addItemDecoration(new EqualSpacingItemDecoration(0));
        recyclerView.setHasFixedSize(true);

//set data and list adapter
        adapterbell = new Adapterbell(getActivity(), recyclerView, data);
        recyclerView.setAdapter(adapterbell);

        recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL));


// on item list clicked
        adapterbell.setOnItemClickListener(( v, obj, position ) -> {


            databaseHandler = new DbHandler(getActivity());

            databaseHandler.updateStatus(data.get(position).getId(), true);


            ((MainActivity) getActivity()).showInterstitialAd();


//            Toast.makeText(getActivity(), String.valueOf(data.get(position).gettitle()), Toast.LENGTH_SHORT).show();


            Intent intent = new Intent(getActivity(), ActivityNotificationDetail.class);
//
            intent.putExtra("id", (data.get(position).getpost_id()));


            Log.v("testti", "" + v.getTag() + " apakah sama " + data.get(position).getpost_id());


            this.startActivity(intent);


        });






        //initShimmerLayout();

        return root_view;
    }


    private void loadDataFromDatabase() {
        databaseHandler = new DbHandler(getActivity());
        data = databaseHandler.getAllbell();

    }


    
    public void onBackPressed() {
        goToHome();
    }

    private void goToHome() {


        Fragment newFragment = new FragmentHome();
        // consider using Java coding conventions (upper first char class names!!!)
        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack
        transaction.replace(R.id.container, newFragment);
        transaction.addToBackStack(null);

        // Commit the transaction
        transaction.commit();

    }

    


}

backpress fragmentbell to fragmenthome not working how to fix this error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The method onBackPressed() in your fragment is not called from anywhere. It doesn’t work the same as in an activity. In the method on create of your fragment, try something like this:

requireActivity().getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
            @Override
            public void handleOnBackPressed() {
                goToHome();
            }
        });

Here are the docs: https://developer.android.com/guide/navigation/navigation-custom-back#implement

However, if you added to the back stack the transaction to show the Fragmentbell then going back should work without having to implement it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading