- ViewModelProviders被弃用,改为ViewModelProvider
ViewModelProvider使用方法
MyViewModel model = new ViewModelProvider(this).get(MyViewModel.class);
可能报错
Cannot resolve constructor 'ViewModelProvider(com.example.viewmodeltest.MainActivity)'
解决方法,在build.gradle的dependencies添加
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
- 使用 FragmentContainerView 创建 NavHostFragment,或通过 FragmentTransaction 手动将 NavHostFragment 添加到 Activity 时,尝试通过 Navigation.findNavController(Activity, @IdRes int) 检索 Activity 的 onCreate() 中的 NavController 将失败。
解决办法是直接从 NavHostFragment中获取NavController。
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.xxx);
NavController controller = navHostFragment.getNavController();
标签:NavController,日记,ViewModelProvider,开发,Activity,navHostFragment,Android,NavHostFr
From: https://www.cnblogs.com/leafz/p/16808255.html