Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cast LifecycleOwner on Service class?

I want to select data on room database with livedata from service class. How to cast the LifecycleOwner when observe it?

repositoryDatabase.getTimeline().observe(this, timelineList -> {
    if (timelineList != null && timelineList.size() >= 10) {
        JSONArray arrayTimeline = new JSONArray();
        for (TimelineEntity timeline : timelineList) {
            JSONObject objectTimeline = new JSONObject();
            try {
                objectTimeline.put("doku", timeline.getIdDokumen());
                objectTimeline.put("entrydate", timeline.getEntryDate());
                objectTimeline.put("lat", timeline.getLat());
                objectTimeline.put("lng", timeline.getLng());

                arrayTimeline.put(objectTimeline);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        onUpdateLocation(arrayTimeline.toString());
    }
});
like image 545
BadGuy Avatar asked Jul 27 '19 06:07

BadGuy


People also ask

How do you use LifecycleOwner?

Every Activity has a Lifecycle. This Activity will be a Lifecycle Owner(any activity or fragment can be called a lifecycle owner). When an activity is initialized LifecycleOwner is the one that will be constructed initially. Any class that implements the LifeCycleOwner interface indicates that it has Android LifeCycle.

What is LifecycleOwner?

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.

How many types of LifecycleOwner available in fragment when and which LifecycleOwner should pass?

When using a ViewModel exposing LiveData s for a Fragment , there are currently two LifecycleOwners that can be used to oberve: the Fragment itself or. the Fragment 's property mViewLifecycleOwner .


2 Answers

You can use LifecycleService like this:

Add this dependency to your app/build.gradle file:

dependencies {
    implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
}

Extend your service with LifecycleService:

class MyService extends LifecycleService {

    ...
}

After that you will be able to observe your LiveData.

like image 169
Birju Vachhani Avatar answered Sep 22 '22 21:09

Birju Vachhani


Add this dependency to your app/build.gradle file:

implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"

As mentioned in the androidx.lifecycle release page

Version 2.2.0 ~~ January 22, 2020 ~~ Important changes since 2.1.0

lifecycle-extensions Artifact Deprecation: With the above deprecation of ViewModelProviders.of(), this release marks the deprecation of the last API in lifecycle-extensions and this artifact should now be considered deprecated in its entirety. We strongly recommend depending on the specific Lifecycle artifacts you need (such as lifecycle-service if you’re using LifecycleService and lifecycle-process if you’re using ProcessLifecycleOwner) rather than lifecycle-extensions as there will not be a future 2.3.0 release of lifecycle-extensions.

like image 32
Nur Ilyas Avatar answered Sep 25 '22 21:09

Nur Ilyas