Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lifecycle OnLifecycleEven tis deprecated

After updating lifecycle library to 2.4.0 Android studio marked all Lifecycle events as deprecated.

@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun create() {
    tts = TextToSpeech(context, this)
}

@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun stopTTS() {
    tts?.stop()
}

Is there any equivalent replacement such as DefaultLifecycleObserver ?

like image 777
Vahe Gharibyan Avatar asked Dec 16 '21 18:12

Vahe Gharibyan


People also ask

Why is onActivityCreated deprecated?

Need for onActivityCreated() deprecation In such similar fashion android developers saw the tight coupling of code dependent to the Activity's life cycle. And they decided that it is not a good practice anymore to be dependent on the activity attach to do certain things inside the fragment.

What is lifecycle owner?

ProcessLifecycleOwner. Class that provides lifecycle for the whole application process. A class that has an Android lifecycle. These events can be used by custom components to handle lifecycle changes without implementing any code inside the Activity or the Fragment.

What is the life cycle in Android?

An Android activity goes through six major lifecycle stages or callbacks. These are: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() . The system invokes each of these callbacks as an activity enters a new state.


3 Answers

It's deprecated because they now expect you to use Java 8 and implement the interface DefaultLifecycleObserver. Since Java 8 allows interfaces to have default implementations, they defined DefaultLifecycleObserver with empty implementations of all the methods so you only need to override the ones you use.

The old way of marking functions with @OnLifecycleEvent was a crutch for pre-Java 8 projects. This was the only way to allow a class to selectively choose which lifecycle events it cared about. The alternative would have been to force those classes to override all the lifecycle interface methods, even if leaving them empty.

In your case, change your class to implement DefaultLifecycleObserver and change your functions to override the applicable functions of DefaultLifecycleObserver. If your project isn't using Java 8 yet, you need to update your Gradle build files. Put these in the android block in your module's build.gradle:

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
like image 151
Tenfour04 Avatar answered Sep 20 '22 04:09

Tenfour04


You class must implement DefaultLifecycleObserver like so

public class MyFavoriteClass implements DefaultLifecycleObserver

Then implement the methods needed (Android Studio: ALT + i)

@Override
public void onResume(@NonNull LifecycleOwner owner) {
    methodRunsAtOnResume();
}

@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
    myFavoriteOnDestroyMethod();
}

In your activity or fragment add this to onCreate()

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MyOtherClass class = new MyOtherClass();
    getLifecycle().addObserver(class);
}

To implement it more correctly create your own observer class, pass the object you like to observe to it. Nice article about it is https://medium.com/@MinaSamy/android-architecture-components-lifecycle-433ace1ec05d

like image 38
S. Gissel Avatar answered Sep 19 '22 04:09

S. Gissel


In order to keep track of the current Activity's lifecycle, you can use the LifecycleEventObserver class. First, create a callback,

private val lifecycleEventObserver = LifecycleEventObserver { source, event ->
    if (event == Lifecycle.Event.ON_RESUME ) {
        Log.e( "APP" , "resumed" )
    }
    else if ( event == Lifecycle.Event.ON_PAUSE ) {
        Log.e( "APP" , "paused" )
    }
}

Attach the callback in the onCreate method of the Activity using lifecycle.add( Observer ) method,

lifecycle.addObserver( lifecycleEventObserver )
like image 35
Shubham Panchal Avatar answered Sep 18 '22 04:09

Shubham Panchal