So, I previously asked a question, and got a bit of help, but ultimately, I decided that I needed to be more clear.
Basically, I have an app. The Main activity is a tabbed activity/swipe view. The first page consists of Main_Fragment, which consists of two other fragments (A and B) within them. Fragment A contains a button, that I am trying to have when pressed, will launch a different fragment to either go ontop of fragment A and B, replace them or Main_fragment.
Where I am getting stuck is what code I would need to use for the Fragment Transaction. Previously I used this code, when Main_Fragment, was just an activity, and it worked out well, but now I am unsure on what I would need to do.
FragmentManager fm = getFragmentManager();
fragment = new DayViewFragment();
FragmentTransaction ft = fm.beginTransaction();
ft.addToBackStack(null);
ft.add(android.R.id.content, fragment);
ft.commit();
Firstly I think I would need to use getParentFragment() somehow, and I am unsure on how, and secondly I am unsure what do replace for android.R.id.content, especially, when the code is inside a fragment inside of a fragment.
If any clarification is needed, please ask. Thanks for any help!
I think the best way to implement this type of functionality is to use an interface callback from the Fragment to the Activity.
Android Studio 1.2.2 will even automatically create interface callback code if you check the "Include interface callbacks?" checkbox when creating a new Fragment.
Here is the general code structure for the Fragment:
public class MainActivityFragment extends Fragment {
private OnMainFragmentInteractionListener mListener;
private Button exampleButton;
public MainActivityFragment() {
}
public static MainActivityFragment newInstance() {
MainActivityFragment fragment = new MainActivityFragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
exampleButton = (Button) rootView.findViewById(R.id.button);
exampleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.buttonClicked("test value");
}
});
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnMainFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnMainFragmentInteractionListener");
}
}
public interface OnMainFragmentInteractionListener {
void buttonClicked(String val);
}
}
Then the Activity will implement the interface:
public class MainActivity extends FragmentActivity
implements MainActivityFragment.OnMainFragmentInteractionListener {
And implement the interface method:
@Override
public void buttonClicked(String val) {
FragmentManager fm = getFragmentManager();
fragment = new DayViewFragment();
FragmentTransaction ft = fm.beginTransaction();
ft.addToBackStack(null);
ft.add(android.R.id.content, fragment);
ft.commit();
}