Android 来自上下文的 FragmentManager

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10689997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:37:24  来源:igfitidea点击:

FragmentManager from Context

android

提问by Steve

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

我创建了一个新View类。在该课程中,我需要访问FragmentManager,但我不知道如何访问。

Can I access the FragmentManagerfrom a context?

我可以FragmentManager从上下文访问吗?

CustomView extends LinearLayout

回答by DeeV

Only if the given Context extends Activity (Post-Honeycomb) or FragmentActivity (pre-honeycomb).

仅当给定的 Context 扩展 Activity (Post-Honeycomb) 或 FragmentActivity (pre-honeycomb) 时。

In which case you'd have to make 100% sure it's an activity using reflection or try-catch.

在这种情况下,您必须 100% 确保它是使用反射或 try-catch 的活动。

try{
  final Activity activity = (Activity) context;

  // Return the fragment manager
  return activity.getFragmentManager();

  // If using the Support lib.
  // return activity.getSupportFragmentManager(); 

} catch (ClassCastException e) {
  Log.d(TAG, "Can't get the fragment manager with this");
}

Thought I recommend refactoring so a Viewis really just meant for showing stuff and shouldn't actually modify the state of your app, but that's my opinion.

我认为我建议重构所以 aView真的只是为了显示内容而不应该实际修改应用程序的状态,但这是我的意见。

回答by clocksmith

if you are using support fragments, you probably actually want:

如果您正在使用支持片段,您可能实际上想要:

try {
  FragmentManager fragmentManager = ((FragmentActivity) context).getSupportFragmentManager();
} catch (ClassCastException e) {
  Log.e(TAG, "Can't get fragment manager");
}

回答by Ojonugwa Jude Ochalifu

This is what worked for me:

这对我有用:

Context mContext;
...


//Get FragmentManager
FragmentManager fragmentManager = ((Activity) mContext).getFragmentManager();

(Of course you have to first of all initialize mContext)

(当然你首先要初始化mContext)

回答by Graeme

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.

您可以访问应用程序中的 FragmentManager(或 SupportFragmentManager) - 但正如其他答案所暗示的那样,您只能通过 Activity 实例执行此操作。

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

但是,您可以通过 Activity 访问 FragmentManager,而无需使用 ActivityLifecycleCallbacks 接口直接调用任何活动:

registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks {

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

    ...

回答by D.M.

Since your context object can't always be directly casted to Activity, this is a more reliable way to do this:

由于您的上下文对象不能总是直接转换为 Activity,因此这是一种更可靠的方法:

@Nullable
public static Activity getActivityFromContext(@NonNull Context context){
    while (context instanceof ContextWrapper) {
        if (context instanceof Activity) return (Activity) context;
        context = ((ContextWrapper)context).getBaseContext();
    }
    return null; //we failed miserably 
}