I have a MainActivity which has a viewpager which contains Host fragments(that can contain other fragments with child fragment manager). What I am trying to achieve is to start a dialog fragment from one of the child fragments on clicking action on the Notification. I am using FCM and the notification is coming properly with the intent, but when I try to add the child fragment, I am getting Fragment not attached to activity while I try to get the child fragment manager.
@Override
protected void onCreate(Bundle savedInstanceState) {
addFrags();//adding fragments to the viewpager
Bundle bundle = getIntent().getExtras();
if (bundle.containsKey(ARG_ACTION_ID)) {
mAction = (Action) bundle.getSerializable(ARG_ACTION_ID);
}
}
@Override
protected void onStart() {
super.onStart();
switch (mAction) {
case EDIT_TFR: {
mViewPager.setCurrentItem(2);//this much is happening, it will go to the 3rd page of viewpager if i didnt do the rest.
onFragmentInteraction(id);
break;
}
}
}
// this is an interface i use to interact with the acticity from my child fragment, which is also the one i am calling from onstart
@Override
public void onFragmentInteraction(String id) {
HostFragment hostFragment = (HostFragment) mPagerAdapter.getItem(2);
Fragment contentfragment = NewFrag.newInstance(id);
hostFragment.addFragment(contentFragment, true);
}
This is the addFragent method inside HostFragment
public void addFragment(Fragment fragment, boolean addToBackstack) {
FragmentManager fm = this.getChildFragmentManager(); //this is were i am getting the error
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(id.hosted_fragment, fragment);
if (addToBackstack) {
fragmentTransaction.addToBackStack((String)null);
}
fragmentTransaction.commit();
fm.executePendingTransactions();
}
I was planning was planning was to call a public method inside the NewFrag which will open up the dialogfrag.
I know the fragment might not be attached by this time. Please let me know how I can achieve this.