Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caused by java.lang.IllegalArgumentException: SavedStateProvider with the given key is already registered #3232

Closed
dzzchao opened this issue Feb 8, 2022 · 4 comments
Assignees

Comments

@dzzchao
Copy link

dzzchao commented Feb 8, 2022

crash log

Caused by java.lang.IllegalArgumentException: SavedStateProvider with the given key is already registered
       at androidx.savedstate.SavedStateRegistry.registerSavedStateProvider(SavedStateRegistry.java:111)
       at androidx.lifecycle.SavedStateHandleController.attachToLifecycle(SavedStateHandleController.java:50)
       at androidx.lifecycle.SavedStateHandleController.create(SavedStateHandleController.java:70)
       at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:67)
       at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:84)
       at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:109)
       at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
       at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
       at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:54)
       at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41)
       at com.xxx.livelibrary.page.live.LiveMainFragment.getVm(LiveMainFragment.kt:61)
       at com.xxx.livelibrary.page.live.LiveMainFragment.init(LiveMainFragment.kt:111)
       at com.xxx.livelibrary.base.LiveBaseFragment.onCreateView(LiveBaseFragment.kt:71)
       at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
       at androidx.fragment.app.DialogFragment.performCreateView(DialogFragment.java:489)
       at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
       at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
       at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
       at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
       at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
       at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3138)
       at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3072)
       at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251)
       at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:502)
       at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
       at com.xxx.livelibrary.page.live.LiveMainActivity.onStart(LiveMainActivity.kt:13)
       at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334)
       at android.app.Activity.performStart(Activity.java:7029)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2752)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2872)
       at android.app.ActivityThread.-wrap11(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1595)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6543)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)

LiveMainActivity

@AndroidEntryPoint
class LiveMainActivity : LiveBaseActivity() {
   override fun instanceFragment(): LiveBaseFragment<*> {
        return LiveMainFragment()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (savedInstanceState != null) {
            finish()
            return
        } else {
            //
        }
    }

}

LiveBaseActivity

abstract class LiveBaseActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    if (savedInstanceState == null) {
            val target = instanceFragment()

            supportFragmentManager
                .beginTransaction()
                .add(R.id.container, target)
                .commitAllowingStateLoss()
        }
  }
}

LiveMainFragment


@AndroidEntryPoint
class LiveMainFragment {
      private val vm: LiveMainViewModel by activityViewModels()
   
  override fun init(inflater: LayoutInflater, targetParent: ViewGroup) {
  vm.xxxxx
}
} 

LiveMainViewModel

@HiltViewModel
class LiveMainViewModel @Inject constructor(private val mainRepository: LiveMainRepository) :
    LiveBaseViewModel() {}

other

implementation "com.google.dagger:hilt-android:2.40.5"
kapt "com.google.dagger:hilt-compiler:2.40.5"

classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40'

Currently, there are many crashes in prod env, and I can't reproduce them.

@sebastinto
Copy link

Have you seen #2328? Looks like it may be related.

A similar issue seems to be happening with Koin. See InsertKoinIO/koin#1207

@dzzchao
Copy link
Author

dzzchao commented Feb 10, 2022

I have seen this issue #2328
But my crash is different from those crashes because I do not use the keyed ViewModel.

@danysantiago
Copy link
Member

Hey @dzzchao, do you have a sample app to reproduce the issue? I can't reproduce it with just the few snippets you have, there is a few unknowns. What is LiveBaseFragment? There is no super.onCreate(savedInstanceState) in LiveBaseActivity. Who calls init in LiveMainFragment? (From the trace it seems to be onCreateView).

You might also want to take a look at https://issuetracker.google.com/175604396 which has some relations to the issues as you describe it but not sure unless there is more info.

@dzzchao
Copy link
Author

dzzchao commented Mar 10, 2022

  1. Sorry, this is a company project.And I can't reproduce this issue either.
  2. Regarding "There is no super.onCreate(savedInstanceState) in LiveBaseActivity", I lost this part of the code in the copy, sorry.
  3. Regarding "Who calls init in LiveMainFragment? (From the trace it seems to be onCreateView)",Yes, it is the onCreateView call init of LiveBaseFragment. I'm missing this part of the detail.

We fixed this in the latest app version, but it's not the right way to do it, if anyone is suffering from online issues, try to use our approach.

override fun onStart() {
    val deleteKeys: List<String?> = arrayListOf<String?>(
        XXViewModel::class.java.canonicalName,
    )
    for (key in deleteKeys) {
        key?.let {
            savedStateRegistry.unregisterSavedStateProvider(key)
        }
    }
    super.onStart()
}

copybara-service bot pushed a commit that referenced this issue Jul 11, 2022
- Fixes issues with keyed ViewModels.
- Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed.
- Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0

Fixes #3232.
Issue #2152.

RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels).
PiperOrigin-RevId: 449844449
copybara-service bot pushed a commit that referenced this issue Jul 11, 2022
- Fixes issues with keyed ViewModels.
- Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed.
- Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0

Fixes #3232.
Issue #2152.

RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK.
PiperOrigin-RevId: 449844449
copybara-service bot pushed a commit that referenced this issue Jul 11, 2022
- Fixes issues with keyed ViewModels.
- Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed.
- Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0

Fixes #3232.
Issue #2152.

RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK.
PiperOrigin-RevId: 449844449
copybara-service bot pushed a commit that referenced this issue Jul 11, 2022
- Fixes issues with keyed ViewModels.
- Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed.
- Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0

Fixes #3232.
Issue #2152.

RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK.
PiperOrigin-RevId: 449844449
copybara-service bot pushed a commit that referenced this issue Jul 12, 2022
- Fixes issues with keyed ViewModels.
- Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed.
- Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0

Fixes #3232.
Issue #2152.

RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK.
PiperOrigin-RevId: 449844449
copybara-service bot pushed a commit that referenced this issue Jul 12, 2022
- Fixes issues with keyed ViewModels.
- Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed.
- Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0

Fixes #3232.
Issue #2152.

RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK.
PiperOrigin-RevId: 449844449
copybara-service bot pushed a commit that referenced this issue Jul 12, 2022
- Fixes issues with keyed ViewModels.
- Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed.
- Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0

Fixes #3232.
Issue #2152.

RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK.
PiperOrigin-RevId: 449844449
copybara-service bot pushed a commit that referenced this issue Jul 12, 2022
- Fixes issues with keyed ViewModels.
- Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed.
- Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0

Fixes #3232.
Issue #2152.

RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK.
PiperOrigin-RevId: 449844449
copybara-service bot pushed a commit that referenced this issue Jul 12, 2022
- Fixes issues with keyed ViewModels.
- Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed.
- Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0

Fixes #3232.
Issue #2152.

RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK.
PiperOrigin-RevId: 449844449
copybara-service bot pushed a commit that referenced this issue Jul 13, 2022
- Fixes issues with keyed ViewModels.
- Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed.
- Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0

Fixes #3232.
Issue #2152.

RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK.
PiperOrigin-RevId: 449844449
copybara-service bot pushed a commit that referenced this issue Jul 13, 2022
- Fixes issues with keyed ViewModels.
- Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed.
- Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0

Fixes #3232.
Issue #2152.

RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK.
PiperOrigin-RevId: 449844449
copybara-service bot pushed a commit that referenced this issue Jul 13, 2022
- Fixes issues with keyed ViewModels.
- Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed.
- Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0

Fixes #3232.
Issue #2152.

RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK.
PiperOrigin-RevId: 449844449
copybara-service bot pushed a commit that referenced this issue Jul 13, 2022
- Fixes issues with keyed ViewModels.
- Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed.
- Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0

Fixes #3232.
Issue #2152.

RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK.
PiperOrigin-RevId: 449844449
copybara-service bot pushed a commit that referenced this issue Jul 13, 2022
- Fixes issues with keyed ViewModels.
- Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed.
- Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0

Fixes #3232.
Issue #2152.

RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK.
PiperOrigin-RevId: 449844449
copybara-service bot pushed a commit that referenced this issue Jul 13, 2022
- Fixes issues with keyed ViewModels.
- Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed.
- Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0

Fixes #3232.
Issue #2152.

RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK.
PiperOrigin-RevId: 449844449
copybara-service bot pushed a commit that referenced this issue Jul 13, 2022
- Fixes issues with keyed ViewModels.
- Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed.
- Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0

Fixes #3232.
Issue #2152.

RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK.
PiperOrigin-RevId: 449844449
copybara-service bot pushed a commit that referenced this issue Jul 14, 2022
- Fixes issues with keyed ViewModels.
- Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed.
- Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0

Fixes #3232.
Issue #2152.

RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK.
PiperOrigin-RevId: 449844449
copybara-service bot pushed a commit that referenced this issue Jul 14, 2022
- Fixes issues with keyed ViewModels.
- Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed.
- Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0
  - That required updating the Android SDK to 31+, so upgrading to 32 and tools to 32.0.0 (besides a bazel example which has issues with that)
  - This requires AGP 7.0.0 so dropping support for AGP 4.2.0
  - AGP 7.0.0 requires Java 11 so dropping support for Java 8
  - Targeting 31+ requires updating androidx.text.core to 1.4.0

Fixes #3232.
Issue #2152.

RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK.
PiperOrigin-RevId: 449844449
copybara-service bot pushed a commit that referenced this issue Jul 14, 2022
- Fixes issues with keyed ViewModels.
- Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed.
- Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0
  - That required updating the Android SDK to 31+, so upgrading to 32 and tools to 32.0.0 (besides a bazel example which has issues with that)
  - This requires AGP 7.0.0 so dropping support for AGP 4.2.0
  - AGP 7.0.0 requires Java 11 so dropping support for Java 8
  - Targeting 31+ requires updating androidx.text.core to 1.4.0

Fixes #3232.
Issue #2152.

RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK.
PiperOrigin-RevId: 449844449
copybara-service bot pushed a commit that referenced this issue Jul 14, 2022
- Fixes issues with keyed ViewModels.
- Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed.
- Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0
  - That required updating the Android SDK to 31+, so upgrading to 32 and tools to 32.0.0 (besides a bazel example which has issues with that)
  - This requires AGP 7.0.0 so dropping support for AGP 4.2.0
  - AGP 7.0.0 requires Java 11 so dropping support for Java 8
  - Targeting 31+ requires updating androidx.text.core to 1.4.0

Fixes #3232.
Issue #2152.

RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK.
PiperOrigin-RevId: 449844449
copybara-service bot pushed a commit that referenced this issue Jul 18, 2022
- Fixes issues with keyed ViewModels.
- Fixes issues with using the ViewModel factory with a navigation entry as owner. Now the lazy function hiltNavGraphViewModels() provided by androidx.hilt:hilt-navigation-fragment is no longer needed.
- Updates deps on fragment to 1.5.0 and lifecycle to 2.5.0
  - That required updating the Android SDK to 31+, so upgrading to 32 and tools to 32.0.0 (besides a bazel example which has issues with that)
  - This requires AGP 7.0.0 so dropping support for AGP 4.2.0
  - AGP 7.0.0 requires Java 11 so dropping support for Java 8
  - Targeting 31+ requires updating androidx.text.core to 1.4.0

Fixes #3232.
Issue #2152.

RELNOTES=Fixes #3232 and #2152 (without hiltNavGraphViewModels). Also, due to the update to the new fragment 1.5.0 version, requires apps using Hilt to use SDK 31+ for the compile SDK.
PiperOrigin-RevId: 449844449
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants