Open a dialog fragment from childfragment from Notification

旧城冷巷雨未停 提交于 2019-12-10 15:59:29

问题


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.

来源:https://stackoverflow.com/questions/54495682/open-a-dialog-fragment-from-childfragment-from-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!