0

I have swipe tabs. On each ot the tab/fragment I want to call a fragment called B (or report). So what I have implemented is within each of the swipe tab fragment I have a button that instantiates the B/report fragment:

private void testReport(){
    FragmentManager fm = getFragmentManager();
    fragt = fm.beginTransaction();
    report = new Report();
    fragt.replace(R.id.container, report);
    fragt.addToBackStack("report");
    fragt.commit();
}

Then on my B/report fragment I have the following code: a button that is supposed to return to the swipe tab fragment and a change counter button.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_report, container, false);
}

int cnt = 0;
TextView txtt;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    final Context context = getActivity().getApplicationContext();

    endt = (Button) getActivity().findViewById(R.id.endt);
    Button chgt = (Button) getActivity().findViewById(R.id.chgt);
    txtt = (TextView) getActivity().findViewById(R.id.txtt);

    endt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            getActivity().getFragmentManager().popBackStack();

        }
    });

    chgt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            cnt++;
            txtt.setText(Integer.toString(cnt));
        }
    });


}

I have the following questions:

Q1 - layout: I thought that inflating a new layout would create a new view. In my app I get the layout of my swipe tab and the layout of the report fragment both active.

Q2 - fragment stack: I thought that addtobackstack and popbackstack allowed to call a fragment B within fragment A, and when the task was finished in fragment B then I could return to fragment A. In my case both fragments are active at the same time even when I do a call to getActivity().getFragmentManager().popBackStack()

narb
  • 958
  • 1
  • 13
  • 39
  • 1
    if you have Fragment inside Fragment why don't you use `getChildFragmentManager()` instead `getFragmentManager()` ? – Selvin Jan 25 '16 at 16:59
  • What do you mean both are active? How `R.id.container` layout xml looks like? If container is inside one of the tabs then seeing fragments on left and right as they were previously plus clicked fragment replaced by report is the correct behavior. – InTwoMinds Jan 25 '16 at 17:12
  • @Selvin - I tried without difference. Can you point me to any doc/tuto? – narb Jan 25 '16 at 17:40
  • @InTwoMides - then I got to do a setvisibility(invisible) for my primary fragment, I assume? – narb Jan 25 '16 at 17:42
  • I'm not sure what you want to achieve. Lets assume you have tabs: 1, 2, and 3. Now you click button in tab 2 to show report fragment. Do you want it to show in place of tab 2 (so you can still swipe, and your fragments are 1, B, 3) OR do you want it to be shown "above" all three tabs and tablayout indicator? – InTwoMinds Jan 25 '16 at 18:49
  • you got it. the first scenario. no need for the "above" scenario. – narb Jan 25 '16 at 18:55
  • This thread shows proper implementation of what you need (for first tab only, but you'll get the idea). The key is to implement FragmentPagerAdapter that remembers what should be where and takes care of FragmentManager operations. http://stackoverflow.com/questions/7723964/replace-fragment-inside-a-viewpager The alternative is to use nested fragments and getChildFragmentManager, but that's more complicated due to nesting. – InTwoMinds Jan 25 '16 at 21:15
  • Many thanks! long thread. interesting. I'll definitely take a look at it. – narb Jan 25 '16 at 21:47

0 Answers0