有没有办法从服务类开始或启动片段

时间:2016-07-28 10:29:41

标签: java android service fragment

以下是我的代码,其中getSupportFragmentManger()在Service类中无法正常工作

 Fragment fragment = new OfferNotificationFragment();
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    fragment = fm.findFragmentByTag("tag6");
    if (fragment == null) {  // not added
        fragment = new OfferNotificationFragment();//add fragment
        ft.replace(R.id.coordinate_layout, fragment, "tag6");
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    } else {  // already added
        ft.remove(fragment);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
    }
    ft.commit();

0 个答案:

没有答案