What is the Difference Between LiveData and LifecycleObserver
I have read the doc on lifecycle and live data in the official Android docs. I know the class has implemented LifeCycleObserver and makes the location listener private or public automatically. I also know that live data can make it active or inactive automatically. I tried to implement Location Observer using these 2 ways. This works and it showed the toast 2 times when the location is updated.
My question is, what is the difference between the two, if I really want to implement something like connecting to a database, GPS location, loading an image, starting a background service. Can I just use the LiveData class? because I only need to implement an active and inactive function.
LocationLiveData.java
public class LocationLiveData extends LiveData<Location> {
private LocationManager locationManager;
private Context context;
public LocationLiveData(Context context) {
this.context = context;
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
}
private LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
setValue(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
@Override
protected void onActive() {
super.onActive();
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
@Override
protected void onInactive() {
super.onInactive();
locationManager.removeUpdates(locationListener);
}
}
MyLocationListener.java
public class MyLocationListener implements LifecycleObserver {
private LocationManager locationManager;
private Context context;
private LocationListener locationListener;
public MyLocationListener(LifecycleActivity lifecycleActivity, LocationListener callback) {
// ...
this.context = lifecycleActivity;
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationListener = callback;
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume() {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause() {
locationManager.removeUpdates(locationListener);
}
}
ComponentActivity.java
public class ComponentActivity extends LifecycleActivity {
public static final int REQUEST_CODE = 200;
private MyLocationListener myLocationListener;
public static class MyLiveData extends ViewModel {
private LocationLiveData locationLiveData;
public void init(Context context) {
locationLiveData = new LocationLiveData(context);
}
public LocationLiveData getLocationLiveData() {
return locationLiveData;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_component);
// Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_CODE);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
//use the live data observer
MyLiveData myLiveData = ViewModelProviders.of(this).get(MyLiveData.class);
myLiveData.init(this);
myLiveData.getLocationLiveData().observe(this, new Observer<Location>() {
@Override
public void onChanged(@Nullable Location s) {
Toast.makeText(ComponentActivity.this, String.format("Lat : %.2f, Lon : %.2f", s.getLongitude(), s.getLatitude()), Toast.LENGTH_SHORT).show();
}
});
//use the life cycle observer
getLifecycle().addObserver(new MyLocationListener(this, new LocationListener() {
@Override
public void onLocationChanged(Location s) {
Toast.makeText(ComponentActivity.this, String.format("Lat : %.2f, Lon : %.2f", s.getLongitude(), s.getLatitude()), Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}));
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 200: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
}
source to share
These are really different things that serve two separate roles. Shortly speaking,
-
LifeCycle
efficient and easy to handle Android lifecycles. It has two main parts.LifecycleOwner
exposes its state changes andLifecycleObserver
listens for those changes to take appropriate steps. -
LiveData
on the other hand uses reactive programming which helps us manipulate data with ease. It has some similarities withStream
inJava 8
andObserver
/ orFlowable
inRxJava
. However, LiveData has the advantage of being lifecycle dependent for Android. Thus, it works closely with the LifeCycle components.
source to share
From a higher level of abstraction, I think that:
- LiveData strong> is a data holder that hosts some data, with the difference that LifeCycle Aware. Thus, it is an observable data holder, not an observer!
- LifecycleObserver , on the other hand, is an observer that takes care of the Lifecycle.
I believe this is the main difference.
source to share
- LifecycleOwner ------ → LifecycleObserver
The arrow above has no data. There LifecyclerOwner , the life cycle of which is open, and then there LifecycleObserver, which will monitor changes in any state LifecycleOwner.
- LifecycleOwner ------ → LiveData -------- → Browser
In the above diagram, the arrows LiveData is a data holder that takes in the context of a LifecycleOwner whose lifecycle should take care of, and then it passes any data changes to an observer (this observer is not a LifecycleObserver, but a data observer ) only if the LifecycleOwner is onStarted or onResumed ...
source to share