AndroidX Activity 结果 API Activity 创建已弃用

AndroidX Activity Result API onActivityCreated Deprecated

在应用级别实现以下依赖后Gradle

implementation "androidx.fragment:fragment:1.3.0-beta02"
implementation "androidx.activity:activity:1.2.0-beta02"

我在 onActivityCreated 方法的片段中发现了以下警告

Overrides deprecated method in 'androidx.fragment.app.Fragment'

实施此方法的新方法可能是什么。下面是我的 onActivityCreated 方法

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mainViewModel = new ViewModelProvider(this).get(MainViewModel.class);
}

我正在使用 requestPermissionLauncher 的依赖项。

您可以在此处更详细地查看 changelog/deprecation 警告:https://developer.android.com/jetpack/androidx/releases/fragment#1.3.0-alpha02

具体

The onActivityCreated() method is now deprecated. Code touching the fragment's view should be done in onViewCreated() (which is called immediately before onActivityCreated()) and other initialization code should be in onCreate().

将您的代码更改为以下示例应该会删除弃用警告:

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mainViewModel = new ViewModelProvider(this).get(MainViewModel.class);
}