-1

I cant' find where it returns in Fragment A after a call to fragment B.

Fragment A calls fragment Test as follows:

btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                FragmentTransaction ft = getFragmentManager().beginTransaction();
Test test = new Test();
                ft.replace(R.id.container, test);
                ft.addToBackStack("test");
                ft.commit();
            }
        });

In Test I click on a button to return:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    Button btn = (Button) getActivity().findViewById(R.id.back);
    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            getActivity().getFragmentManager().popBackStackImmediate();
        }
    });
}

From there I thought that it was returning to Fragment A in onAttach, onResume but I don't see where it is returning in Fragment A?

   @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }
narb
  • 958
  • 1
  • 13
  • 39

2 Answers2

0

PopBackStack() doesn't pop a fragment, it pops a fragment transaction.

So the last fragment transaction is reversed upon being called. If you are moving from FragmentA to FragmentTest then it would replace FragmentA with FragmentTest and add that transaction to the back stack(not the fragment). If you then hit the back button, it pops the transaction off the back stack, which was "replace this FragmentA with a FragmentTest".

This instruction reverses the last transaction and removes it from the stack of transactions carried out. So onAttach wont be called and since your are replacing the fragment oncreateView will be called.

Edit:Use this function to check backstack is present and has your test fragment.

if (getSupportFragmentManager().getBackStackEntryCount() > 0 && getSupportFragmentManager().findFragmentById(R.id.your_container)!=null) {
     Fragment fragment= getSupportFragmentManager().findFragmentById(R.id.your_container);
      if(fragment.getClass().getSimpleName().equalsIgnoreCase(FragmentTest.class.getSimpleName())){
          getSupportFragmentManager().popBackStackImmediate();
        }
    }
Sharath kumar
  • 4,064
  • 1
  • 14
  • 20
  • I thought it was used to maintain an active fragments stack! So how do I terminate fragment test and resume A. I would prefer to use an UI back button. – narb Sep 06 '17 at 14:42
  • I dint understand the problem.When you call popbackstack() the fragment test will be terminated and fragment A on create view is called following other life cycles.So obviously fragment A on resume will be called.If you find my explanation helpful please up vote and accept answer. – Sharath kumar Sep 07 '17 at 04:58
  • when you say: "it pops the transaction off the back stack, which was "replace this FragmentA with a FragmentTest", I actually disagree. – narb Sep 07 '17 at 14:16
  • I rewind: my comprehension was correct. the effect of the popbackstack was in fact to get back to the frag A screen. – narb Sep 07 '17 at 14:18
  • Thats what I explained.Just read the sentence properly and understand before down voting. – Sharath kumar Sep 08 '17 at 04:32
  • But onResume (which I mentioned) is and it should have except that the place holder was wrong. I did not provide enough code for you to really guess what was the issue but I cannot agree you spotted the problem. It could be misleading for others. – narb Sep 08 '17 at 05:55
  • The information you give for your question is too less and how could you expect anyone to figure out everything.You should be very clear while asking questions. – Sharath kumar Sep 08 '17 at 06:19
-1

Use

Log.d("Fragment", "Fragment A")

in onResume() method of Fragment A class. If control returns to Fragment A then this line will be shown in logcat.

Tavinder Singh
  • 380
  • 1
  • 7
  • 17
  • I'm in debugger mode with a break point in onResume. it does not get back to onResume. – narb Sep 06 '17 at 08:16
  • use popBackStack("test") instead of popBackStackImmediate() – Tavinder Singh Sep 06 '17 at 08:25
  • popbackstack("test",0) doesn't work better. I changed to addToBackStack(null) and a popbackstack without tag. I added a detach and a pause. I see frag test going through both but no sign of re-activation in frag A. – narb Sep 06 '17 at 09:07