从通知中的子片段打开对话框片段

时间:2019-02-02 17:38:21

标签: android android-fragments push-notification firebase-cloud-messaging child-fragment

我有一个MainActivity,它的viewpager包含Host片段(可以包含带有子片段管理器的其他片段)。我想要实现的是在单击通知上的操作时从子片段之一启动对话框片段。我正在使用FCM,并且通知正按照意图正确发送,但是当我尝试添加子片段时,我尝试获取子片段时得到片段未附加到活动经理

@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);
}

这是HostFragment内部的addFragent方法

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

我当时计划的是计划在NewFrag内部调用一个公共方法,这将打开dialogfrag。

我知道这段时间可能尚未附加该片段。请让我知道我如何实现这一目标。

0 个答案:

没有答案