FragmentManager from Context

后端 未结 5 1182
旧时难觅i
旧时难觅i 2020-12-15 03:09

I created a new View class. Within that class I need to get access to the FragmentManager, but I cannot figure out how.

Can I access the

5条回答
  •  青春惊慌失措
    2020-12-15 03:40

    You can get access to a FragmentManager (or SupportFragmentManager) in an Application - but as other answers suggests, you can only do this via an Activity instance.

    However, you can gain access to a FragmentManager via an Activity without needing to directly call any Activities using the ActivityLifecycleCallbacks interface:

    registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks {
    
        @Override
        public void onActivityCreated(Activity activity, Bundle bundle) {
            activity.getFragmentManager()
            if(activity instanceof FragmentActivity) {
                ((FragmentActivity)activity).getSupportFragmentManager();
            }
            unregisterActivityLifecycleCallbacks(this);
        }
    
        ...
    

提交回复
热议问题