Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android lifecycle library: Cannot add the same observer with different lifecycles

I have an app I'm working on that's using the lifecycle library but I'm getting an IllegalArgumentException that says "Cannot add the same observer with different lifecycles" I only add observers in onCreate which I thought would be safe. Most of my observers are added via anonymous classes which I assume can't be the issue here since the observer is never recycled. One is using this:

private GpsState gpsState;  void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     getLifecycle().addObserver(gpsState);     gpsState.observe(this, (state) -> {         // ...     }); } 

In this example GpsState extends LiveData to provide the current state of the GPS and implements LifecycleObserver to be able to refresh certain values when reaching an ON_RESUME state.

Any idea what I might be doing wrong?

like image 344
TheHebrewHammer Avatar asked Oct 30 '17 22:10

TheHebrewHammer


2 Answers

In my case the problem was at lambda method of observer is empty. I just tried to add something to it and problem was solved. For example:

gpsState.observe(this, (state) -> {                 Log.d(this.getClass().getSimpleName(), BaseNavigationActivity.this.toString());  }); 

Most likely that JVM define anonymous classes that use only static references and for such cases it become kinda singleton, so you will have same instance all the time you reference such class.

like image 149
Nokuap Avatar answered Sep 19 '22 13:09

Nokuap


As thehebrewhammer said in a comment, I had the same issue because of Kotlin SAM-Lambda optimization.

viewModel.myLiveData.observe(this, Observer {     NavigationBackEvent().post() }) 

This SAM-Lambda doesn't access anything of the class and will be compiled to a singleton for optimization.
I changed it to a class initialization for forcing new instance at each run:

viewModel.myLiveData.observe(this, MyObserver()) 

and

class MyObserver : Observer<MyType?> {     override fun onChanged(it: MyType?) {         NavigationBackEvent().post()     } } 
like image 39
Kevin Robatel Avatar answered Sep 16 '22 13:09

Kevin Robatel