Search code examples
javaandroidandroid-4.2-jelly-bean

Return previous screen not working


I have two screens in my app, let's say screen A and screen B. Please see my code below.

screen A -> screen B:

  EditProfileFragment contactFragment = new EditProfileFragment();
            android.app.FragmentTransaction contactTransaction = getActivity().getFragmentManager().beginTransaction();
            contactTransaction.add(R.id.fragment_container, contactFragment, GlobalVariable.MyPROFILE_EDIT_FRAGMENT);
            contactTransaction.addToBackStack(null);

            contactTransaction.commit();

after saving the record, I want to return to previous screen B -> A:

getFragmentManager().popBackStackImmediate();

But it's not working. Nothing else happens. I've been googling for a few hours but I'm still not able to find any solution.


Solution

  • If i'm not wrong you're trying to return previous fragment. So why don't you just use:

    getFragmentManager().popBackStack();
    

    I edit your code: (User SupportFragment and SupportFragmentManager)

      EditProfileFragment contactFragment = new EditProfileFragment();
                FragmentTransaction contactTransaction = getActivity().getSupportFragmentManager().beginTransaction();
                contactTransaction.replace(R.id.fragment_container, contactFragment, GlobalVariable.MyPROFILE_EDIT_FRAGMENT);
                contactTransaction.addToBackStack(null);
                contactTransaction.commit();
    

    And use when you save record:

    getSupportFragmentManager().popBackStack();