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()