0

I am having 4 fragments.They are consists of HomeFragment, Rainfall, rainday, and yieldFragment. If right now i am on Rainfall fragment, then i press backbutton, i want the fragment will come back to Home fragment(as a default). But right now my applicaton situation is when press backbutton, the application was closed. I've tried by using onBackpressed, but i think my onBackPressed was not work properly.

What should i do for this situation..?Please anyone can answer. i've tried this...

  public void onBackPressed() {

        fragment=new HomeFragment();}

Please help me if anyone knows.

Sasya
  • 85
  • 1
  • 9

3 Answers3

2

If I'm not mistaken, all you have to do is paste the following line of code into the onViewCreated fun of the relevant fragment.

        val callback = object : OnBackPressedCallback(true){
            override fun handleOnBackPressed() {
                "your_code_here"
                "If you leave this blank, the back button will not work."
            }
        }

        requireActivity().onBackPressedDispatcher.addCallback(callback)
0
  • first of all at the time of switch in to fragment ,used #replace rather then #add new fragment from your container activity.
  • second inside your #inner fragments ,override onResume() as depicted below. It will give you call back of device back button .

    @Override public void onResume() {

        super.onResume();
    
        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
        getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
    
                if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
    
                    HomeFragment fragObj = new HomeFragment();
    
                    getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragObj)
                            .commit();
    
                    return true;
                }
    
                return false;
            }
        });
    }
    
Radhey
  • 2,139
  • 2
  • 24
  • 42
  • can i ask you what the refer of (R.id.fragment_container)? :) – Sasya Oct 12 '16 at 02:35
  • R.id.fragment_container ,is the id of your container activity (main activity / home activity )in which all fragments will be loaded.Normally we used frameLayout as a container in side main activity and this id refers that frameLayout. – Radhey Oct 12 '16 at 04:58
0

Add your fragment transaction to the back stack, like so:

getSupportFragmentManager().beginTransaction()
           .replace(R.id.your_fragment_id, RainfallFragment())
           .addToBackStack(null)
           .commit()

If you're extending AppCompatActivity and using the latest version of support library, you don't have to do anything else. But if that's not the case, override onBackPressed() in your activity:

@Override
public void onBackPressed() {
     if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
           getSupportFragmentManager().popBackStack();
     } else {
           finish();
     }
}
Jagoan Neon
  • 1,072
  • 11
  • 13