当前位置:网站首页>MVVM framework part I lifecycle
MVVM framework part I lifecycle
2022-07-05 11:57:00 【asahi_ xin】
One .LifeCycle Introduce
Decoupling plays an important role in software development , Normal components need to rely on the life cycle of system components in the process of use . stay onCreate or onResume Method initialization , stay onPause Stop in , stay onDestroy Resource recycling in . This may cause memory leaks .LifeCycle It was born for this , You can strengthen the management of custom components , Callback methods that do not depend on the page lifecycle .
Two . Easy to use
Many of our needs are in onResume To initialize ,onPause To stop . Such as NFC Or get the geographical location
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
Log.d("=====","onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.d("=====","onPause");
}
}
Jetpack Provide us with two classes ,LifecycleOwner( Observed ) and LifecycleObserver( The observer ).
Activity It's done LifecycleOwner Interface , We just need to achieve LifecycleObserver that will do .
public class MyMainListener implements LifecycleObserver {
private OnMainChangeListener listener;
public MyMainListener(OnMainChangeListener listener) {
this.listener = listener;
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
private void myResume() {
listener.onChange("onResume");
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
private void myPause() {
listener.onChange("onPause");
}
public interface OnMainChangeListener {
void onChange(String message);
}
}
Use... In method @OnLifecycleEvent(Lifecycle.Event.ON_XXX) Label for identification . When the life cycle changes periodically , Method will automatically call .
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyMainListener listener = new MyMainListener(new MyMainListener.OnMainChangeListener() {
@Override
public void onChange(String message) {
Log.d("=====", message);
}
});
getLifecycle().addObserver(listener);
}
}
stay service aspect ,Android Provide LifecycleService, Inheritance of such kind Service, Realization LifecycleOwner Interface , Usage and Activity similar .
Add dependency
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
public class MyServiceObserver implements LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
private void myCreate() {
Log.d("=====", "create");
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
private void destroy() {
Log.d("=====", "destroy");
}
}
public class MyService extends LifecycleService {
public MyService() {
MyServiceObserver myServiceObserver = new MyServiceObserver();
getLifecycle().addObserver(myServiceObserver);// Add observers
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.start).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
}
});
findViewById(R.id.stop).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this, MyService.class);
stopService(intent);
}
});
}
}
3、 ... and .ProcessLifecycleOwner
There are also system components with a lifecycle Application.Android It also provides ProcessLifecycleOwner To monitor the application lifecycle .
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
public class ApplicationObserver implements LifecycleObserver {
/** * ON_CREATE It will only be called once in the entire life cycle of the application */
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void onCreate() {
Log.d("=====", "Lifecycle.Event.ON_CREATE");
}
/** * When the application appears in the foreground, call */
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart() {
Log.d("=====", "Lifecycle.Event.ON_START");
}
/** * When the application appears in the foreground, call */
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume() {
Log.d("=====", "Lifecycle.Event.ON_RESUME");
}
/** * Called when the application exits to the background */
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause() {
Log.d("=====", "Lifecycle.Event.ON_PAUSE");
}
/** * Called when the application exits to the background */
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop() {
Log.d("=====", "Lifecycle.Event.ON_STOP");
}
/** * Will never be called to , The system does not distribute calls ON_DESTROY event */
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroy() {
Log.d("=====", "Lifecycle.Event.ON_DESTROY");
}
}
public class BaseApp extends Application {
@Override
public void onCreate() {
super.onCreate();
ProcessLifecycleOwner.get().getLifecycle().addObserver(new ApplicationObserver());
}
}
With ProcessLifecycleOwner, We can monitor the application lifecycle , But there are a few points to note .
- ProcessLifecycleOwner It is the monitoring of the whole application , And Activity The quantity of is irrelevant .
- ON_CREATE It will only be called once ,ON_DESTROY Although there are methods, they will not call .
边栏推荐
- 《看完就懂系列》15个方法教你玩转字符串
- 【load dataset】
- Solve the grpc connection problem. Dial succeeds with transientfailure
- [pytorch pre training model modification, addition and deletion of specific layers]
- Use and install RkNN toolkit Lite2 on itop-3568 development board NPU
- Halcon 模板匹配实战代码(一)
- C operation XML file
- Open3d mesh (surface) coloring
- 简单解决redis cluster中从节点读取不了数据(error) MOVED
- [calculation of loss in yolov3]
猜你喜欢
随机推荐
多表操作-自关联查询
自动化测试生命周期
Crawler (9) - scrape framework (1) | scrape asynchronous web crawler framework
简单解决redis cluster中从节点读取不了数据(error) MOVED
Codeworks 5 questions per day (1700 average) - day 5
COMSOL -- establishment of geometric model -- establishment of two-dimensional graphics
ACID事务理论
redis的持久化机制原理
阻止浏览器后退操作
Empêcher le navigateur de reculer
Thoughts and suggestions on the construction of intelligent management and control system platform for safe production in petrochemical enterprises
Reading notes of growth hacker
[untitled]
vscode快捷键
2022年国内云管平台厂商哪家好?为什么?
[yolov5.yaml parsing]
Hash tag usage in redis cluster
Simply solve the problem that the node in the redis cluster cannot read data (error) moved
SET XACT_ ABORT ON
Network five whip