当前位置: 代码迷 >> 综合 >> Jetpack--LifeCycle、ViewModel、LiveData
  详细解决方案

Jetpack--LifeCycle、ViewModel、LiveData

热度:48   发布时间:2023-09-14 05:39:18.0

Jetpack是google官方的安卓开发工具集,目的是为了标准化和加快开发效率,并且之后会持续更新

Jetpack--LifeCycle、ViewModel、LiveData

安卓开发中,google推荐使用MVVM架构,Jetpack集成了构建MVVM架构的几种工具,相比于以前的MVVM,组合使用这些工具会更加高效、简洁、安全。可以说你的安卓项目没有升级使用这套架构,那么你的架构就已经过时了

JetPack与AndroidX

  • AndroidX命名空间中包含Jetpack库
  • AndroidX代替Android Support Library
  • AAC(Android Architect Component)中的组件并入AndroidX
  • 其他一些需要频繁更新和迭代的特性也并入AndroidX

一、LifeCycle

LifeCycle会自动绑定组件的生命周期,省去了我们以前在onResume,onPause等方法中的操作
Jetpack--LifeCycle、ViewModel、LiveData
1.LifeCycle解耦页面与组件

我们有一个Activity,布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><Chronometerandroid:id="@+id/chronometer"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="(0):(0)"android:textSize="24sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>

这是一个简单的计时器,我们想要在Activity处于前台时计时,退到后台暂停计时,那么Activity中写法如下:

package com.aruba.jetpackapplication;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.os.SystemClock;
import android.widget.Chronometer;public class MainActivity extends AppCompatActivity {private Chronometer chronometer;private long countTime;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);chronometer = findViewById(R.id.chronometer);}@Overrideprotected void onResume() {super.onResume();//恢复计时,基于休息的时间作一个偏移chronometer.setBase(SystemClock.elapsedRealtime() - countTime);chronometer.start();}@Overrideprotected void onPause() {super.onPause();//记录下计时时间countTime = SystemClock.elapsedRealtime() - chronometer.getBase();chronometer.stop();}
}

效果:

Jetpack--LifeCycle、ViewModel、LiveData
接下来是使用LifeCycle方式:

1.实现LifecycleObserver接口
2.在方法上添加注解,指定在什么时候执行

package com.aruba.lifecycle;import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.widget.Chronometer;import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;/*** Created by aruba on 2021/9/9.*/
class MyChronometer extends Chronometer implements LifecycleObserver {//实现LifecycleObserver接口private long countTime;public MyChronometer(Context context, AttributeSet attrs) {super(context, attrs);}//对应生命周期的注解@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)private void startCount() {//恢复计时,基于休息的时间作一个偏移setBase(SystemClock.elapsedRealtime() - countTime);start();}//对应生命周期的注解@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)private void stopCount() {//记录下计时时间countTime = SystemClock.elapsedRealtime() - getBase();stop();}}

把布局文件改为MyChronometer 后,在Activity中添加一行监听代码

public class MainActivity2 extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main2);MyChronometer chronometer = findViewById(R.id.chronometer);getLifecycle().addObserver(chronometer);}
}

效果:

Jetpack--LifeCycle、ViewModel、LiveData
2.使用LifecycleService解耦Service与组件

首先需要添加下依赖

implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

定义一个类,实现LifecycleObserve接口,并实现gps数据获取

package com.aruba.lifecycle;import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;/*** Created by aruba on 2021/9/9.*/
public class MyLocationObserver implements LifecycleObserver {private Context context;private LocationManager locationManager;private MyLocation listener;public MyLocationObserver(Context context) {this.context = context;}/*** 开启gps*/@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)private void startGetLocation() {locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {return;}listener = new MyLocation();locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300, 1, listener);}/*** 关闭gps*/@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)private void stopGetLocation() {locationManager.removeUpdates(listener);}static class MyLocation implements LocationListener {private static final String TAG = MyLocation.class.getSimpleName();@Overridepublic void onLocationChanged(@NonNull Location location) {Log.i(TAG, location.toString());}@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {}@Overridepublic void onProviderEnabled(@NonNull String provider) {}@Overridepublic void onProviderDisabled(@NonNull String provider) {}}
}

权限在manifests.xml里也要添加
使用一个service来获取gps数据,继承于LifecycleService,并在相关方法上写上注解

package com.aruba.lifecycle;import android.app.Service;
import android.content.Intent;
import android.os.IBinder;import androidx.lifecycle.LifecycleService;public class GpsService extends LifecycleService {public GpsService() {MyLocationObserver myLocationObserver = new MyLocationObserver(this);getLifecycle().addObserver(myLocationObserver);}
}

模拟器可以使用adb命令修改下gps位置

adb -s  Pixel2:5554 emu geo fix 121.4961236714487 31.24010934431376
adb -s  Pixel2:5554 emu geo fix 122.4961236714487 31.24010934431376

Activity中开启测试下效果:

2021-09-09 17:05:03.316 3046-3046/com.aruba.lifecycle I/MyLocation: Location[gps 37.421998,-122.084000 acc=20 et=+1m13s108ms alt=0.0 {Bundle[EMPTY_PARCEL]}]
3.ProcessLifecycleOwner监听应用程序生命周期

新建一个类实现LifecycleObserve,在方法上加上注解,指定想要监听的生命周期

package com.aruba.lifecycle;import android.util.Log;import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;/*** Created by aruba on 2021/9/9.*/
class AppLifeObserve implements LifecycleObserver {private static final String TAG = AppLifeObserve.class.getSimpleName();@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)private void onCreate() {Log.i(TAG, "onCreate");}@OnLifecycleEvent(Lifecycle.Event.ON_START)private void onStart() {Log.i(TAG, "onCreate");}@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)private void onResume() {Log.i(TAG, "onResume");}@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)private void onPause() {Log.i(TAG, "onPause");}@OnLifecycleEvent(Lifecycle.Event.ON_STOP)private void onStop() {Log.i(TAG, "onStop");}@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)private void onDestroy() {Log.i(TAG, "onDestroy");}}

在Application中,使用ProcessLifecycleOwner注册观察

package com.aruba.lifecycle;import android.app.Application;import androidx.lifecycle.ProcessLifecycleOwner;/*** Created by aruba on 2021/9/9.*/
class MyApp extends Application {@Overridepublic void onCreate() {super.onCreate();ProcessLifecycleOwner.get().getLifecycle().addObserver(new AppLifeObserve());}
}

其中onCreate只会调用一次,onDestroy不会调用

Lifecycle可以使我们不必在原来组件的生命周期中进行管理,降低了模块的耦合度,一定程度上避免了没有及时销毁资源的情况,降低了内存泄漏的发生

二、ViewModel

Jetpack中,官方提供了ViewModel组件,我们应该继承它实现我们的ViewModel层业务
1.瞬态数据保存

例如以前我们手机屏幕旋转时,如果没有配置,那么Activity会重新创建,数据就会丢失
使用ViewModel,我们可以什么都不做就解决这个问题
现在来创建一个Activity,点击按钮让一个数字不断加一,布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="0"android:textSize="30sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.287" /><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="100dp"android:text="add"android:onClick="addCount"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.498"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/textView"app:layout_constraintVertical_bias="0.133" /></androidx.constraintlayout.widget.ConstraintLayout>

创建一个类继承ViewModel

package com.aruba.viewmodel;import androidx.lifecycle.ViewModel;/*** Created by aruba on 2021/9/10.*/
class NumberViewModel extends ViewModel {public int number;
}

在Activity中使用ViewModelProvider通过反射获取ViewModel,并实现点击方法

package com.aruba.viewmodel;import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;import android.os.Bundle;
import android.view.View;
import android.widget.TextView;public class MainActivity extends AppCompatActivity {private TextView textView;private NumberViewModel numberViewModel;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);numberViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(NumberViewModel.class);textView = findViewById(R.id.textView);textView.setText(String.valueOf(numberViewModel.number));}public void addCount(View view) {numberViewModel.number++;textView.setText(String.valueOf(numberViewModel.number));}
}

效果:

Jetpack--LifeCycle、ViewModel、LiveData

如果要在ViewModel中使用Context,不要手动传入,而是继承至AndroidViewModel

package com.aruba.viewmodel;import android.app.Application;import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.ViewModel;/*** Created by aruba on 2021/9/10.*/
public class NumberViewModel extends AndroidViewModel {public int number;public NumberViewModel(@NonNull Application application) {super(application);}
}
2.除了瞬态数据自动保存外,ViewModel还具有异步调用不会造成内存泄漏的优点,需要结合LiveData使用,ViewModel的生命周期是独立于Activity的
Jetpack--LifeCycle、ViewModel、LiveData

三、LiveData

LiveData对象提供了可观测方法,当数据发送改变时,观测方能够观测到,并且线程安全,集成了LifeCycle的绑定生命周期特性
1.来实现一个定时器,线程中更新定时时间,使用LiveData使得ui上进行更新

布局文件很简单,一个TextView

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><TextViewandroid:id="@+id/countTextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="0"android:textSize="30sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>

定义ViewModel,并使用LiveData

package com.aruba.livedata;import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;/*** Created by aruba on 2021/9/10.*/
public class CountViewModel extends ViewModel {private MutableLiveData<Integer> count;public MutableLiveData<Integer> getCount() {if (count == null) {count = new MutableLiveData<>();count.setValue(0);}return count;}
}

在Activity中使用ViewModel,并观测LiveData的值,ui线程中使用setValue方法设置LiveData的值,非ui线程使用postValue方法

package com.aruba.livedata;import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;import android.os.Bundle;
import android.view.View;
import android.widget.TextView;import java.sql.Time;
import java.util.Timer;
import java.util.TimerTask;public class MainActivity extends AppCompatActivity {private TextView countTextView;private CountViewModel countViewModel;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);countViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(CountViewModel.class);countTextView = findViewById(R.id.countTextView);countViewModel.getCount().observe(this, new Observer<Integer>() {@Overridepublic void onChanged(Integer integer) {countTextView.setText(String.valueOf(integer));}});startTimer();}private void startTimer() {new Timer().schedule(new TimerTask() {@Overridepublic void run() {//setValue:ui线程中使用//postValue:非ui线程中使用countViewModel.getCount().postValue(countViewModel.getCount().getValue() + 1);}}, 0, 1000);}}

效果:

Jetpack--LifeCycle、ViewModel、LiveData

四、ViewModel+LiveData,实现Fragment间通信

先看效果:

Jetpack--LifeCycle、ViewModel、LiveData

定义两个fragment,布局是一样的

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".FirstFragment"><SeekBarandroid:id="@+id/seekbar"android:layout_width="0dp"android:layout_height="100dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>

创建ViewModel,定义要联动的进度值

package com.aruba.livedata2;import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;/*** Created by aruba on 2021/9/10.*/
public class MyViewModel extends ViewModel {private MutableLiveData<Integer> progress;public MutableLiveData<Integer> getProgress() {if (progress == null) {progress = new MutableLiveData<>();progress.setValue(0);}return progress;}
}

实现两个fragment中对ViewModel进度值的观察,注意这边获取ViewModel时Owner要用Activity的上下文,因为实现联动需要使用同一个ViewModel,不同Owner会生成不同的实例

package com.aruba.livedata2;import android.os.Bundle;import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;public class FirstFragment extends Fragment {private SeekBar seekbar;private MyViewModel myViewModel;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View root = inflater.inflate(R.layout.fragment_first, container, false);seekbar = root.findViewById(R.id.seekbar);myViewModel = new ViewModelProvider(getActivity(), new ViewModelProvider.AndroidViewModelFactory(getActivity().getApplication())).get(MyViewModel.class);myViewModel.getProgress().observe(this, new Observer<Integer>() {@Overridepublic void onChanged(Integer integer) {seekbar.setProgress(integer);}});seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {@Overridepublic void onProgressChanged(SeekBar seekBar, int i, boolean b) {myViewModel.getProgress().setValue(i);}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {}});return root;}
}

第二个fragment代码是一样的。然后在Activity中加载两个fragment

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><androidx.constraintlayout.widget.Guidelineandroid:id="@+id/guideline2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"app:layout_constraintGuide_percent="0.5" /><fragmentandroid:id="@+id/fragmentContainerView"android:name="com.aruba.livedata2.FirstFragment"android:layout_width="0dp"android:layout_height="0dp"app:layout_constraintBottom_toTopOf="@+id/guideline2"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><fragmentandroid:id="@+id/fragmentContainerView2"android:name="com.aruba.livedata2.SecondFragment"android:layout_width="0dp"android:layout_height="0dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="@+id/guideline2" /></androidx.constraintlayout.widget.ConstraintLayout>
Demo地址:https://gitee.com/aruba/my-jetpack-application.git