8

I'm building a simple app and I want to go back from fragment to activity with physical button. How do I do that? I have tried to kill the fragment but it isn't working.

Eenvincible
  • 5,641
  • 2
  • 27
  • 46
MilanNz
  • 1,323
  • 3
  • 12
  • 29
  • You can save state of fragment [like this][1] [1]: http://stackoverflow.com/questions/11353075/how-can-i-maintain-fragment-state-when-added-to-the-back-stack – R World Aug 30 '14 at 20:24

4 Answers4

15

You can get a reference to the FragmentActivity by calling getActivity() on your current Fragment and then call from the Activity retrieved onBackPressed() method.

getActivity().onBackPressed();
Víctor Albertos
  • 8,093
  • 5
  • 43
  • 71
13

I do it like this and it's working very fine.

Start fragment:

 public void showMyFragment(View V){
            Fragment fragment = null;
            fragment = new MyFragment();

            if (fragment != null) {
                 FragmentManager fragmentManager = getFragmentManager();
                 fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).addToBackStack(null).commit();
            }   
    }

This is how to end fragment with back button:

 @Override
    public void onBackPressed() {
        if (getFragmentManager().getBackStackEntryCount() == 0) {
            this.finish();
        } else {
            getFragmentManager().popBackStack();
        }
    }

Your Fragment:

public class MyFragment extends Fragment {

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
                View v=inflater.inflate(R.layout.activity_info, null);
                return v;
            }
        }
MilanNz
  • 1,323
  • 3
  • 12
  • 29
0

Show fragment with this:

getSupportFragmentManager().beginTransaction()
                    .setReorderingAllowed(true)
                    .replace(R.id.frame_container,fragment.class,null).addToBackStack("desc_fragment")
                    .commit();

and then override onBackPressed in your activity:

@Override
public void onBackPressed() {
    if(getSupportFragmentManager().getBackStackEntryCount() > 0)
        getSupportFragmentManager().popBackStack();
    else
        super.onBackPressed();
}
Saeid Mohammadi
  • 319
  • 1
  • 2
  • 11
0

Image

If you want to simply move fragment to Activity on Toolbar backclick button then use it inside of onCreateView

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

    // Inflate the layout for this fragment
    View view= inflater.inflate(R.layout.fragment_email, container, false);
    EmailFragment=view.findViewById(R.id.EmailFragment);

    backArrow=view.findViewById(R.id.back_arrow);
    backArrow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            requireActivity().onBackPressed();
        }
    });

    return view;
}
user16217248
  • 3,119
  • 19
  • 19
  • 37
Raja Mali
  • 15
  • 5