且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在分离的片段中获取上下文/活动?

更新时间:2023-01-13 11:52:48

在你的片段中使用这个:

Use this in your fragment:

private Context context;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    context = activity;
}

然后在需要的地方使用 context.

then use context where you want.