has19
has19

Reputation: 1317

invalid parameter type. Must be one and instanceof LifecycleOwner

I am trying to use the lifeCycleOwner and lifecycleObserve . I am getting the following exception when i call lifecycleOwner.getLifecycle().addObserver(this);

invalid parameter type. Must be one and instanceof LifecycleOwner

the code as shown below

public class TestActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_activity);

        new  TestObserver(this,this);
    }   
    }

public class TestObserver implements LifecycleObserver{

  public TestObserver(LifecycleOwner lifecycleOwner, Context context){
        lifecycleOwner.getLifecycle().addObserver(this);
    }
}

Edited: as requested the Build.gradle file

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.test"
        minSdkVersion 14
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }


    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:cardview-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'

    // ViewModel and LiveData ,lifecycleObserver
    implementation 'android.arch.lifecycle:extensions:1.1.1'
    implementation "android.arch.lifecycle:runtime:1.1.1"

}

apply plugin: 'com.google.gms.google-services'
apply plugin: 'io.fabric'

Upvotes: 6

Views: 2273

Answers (3)

user1185087
user1185087

Reputation: 4848

The marked answer is not correct.

It is allowed to expect as first argument a parameter of type LifecycleOwner. Optional it is possible to add as second argument, the Lifecycle.Event. In the second case, the annotation must be Lifecycle.Event.ON_ANY. Here are both working options:

@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
private fun onCreate(owner: LifecycleOwner) {}

@OnLifecycleEvent(Lifecycle.Event.ON_ANY) // << Must be ON_ANY
private fun onAnyEvent(owner: LifecycleOwner, event: Lifecycle.Event) {}

Explanation see source code of androidx.lifecycle.ClassesInfoCache: enter image description here

Upvotes: 3

User_5635
User_5635

Reputation: 246

When you override onCreate or other events in the LifecycleObserver, don't pass any argument to the method.

This cause your error :

@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
protected void onCreate(Bundle savedInstanceState)

This is the right declaration :

@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
protected void onCreate()

Upvotes: 23

Sandip
Sandip

Reputation: 293

This is your Observer Class:

public class TestObserver implements LifecycleObserver {

private Activity activity;


public void ActivityRecognitionHelper(LifecycleOwner lifecycleOwner, Activity activity){
    this.activity = activity;
    lifecycleOwner.getLifecycle().addObserver(this);
}

}

You can call above Observer Class from any activity in following way:

new TestObserver().ActivityRecognitionHelper(this, this);

Upvotes: 0

Related Questions