当前位置:网站首页>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 .
边栏推荐
- redis主从模式
- JS for循环 循环次数异常
- [loss functions of L1, L2 and smooth L1]
- MySQL statistical skills: on duplicate key update usage
- [crawler] Charles unknown error
- 871. Minimum Number of Refueling Stops
- Redis master-slave mode
- Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in
- C # implements WinForm DataGridView control to support overlay data binding
- Linux Installation and deployment lamp (apache+mysql+php)
猜你喜欢
随机推荐
Redis集群(主从)脑裂及解决方案
Liunx prohibit Ping explain the different usage of traceroute
2048 game logic
redis的持久化机制原理
Which domestic cloud management platform manufacturer is good in 2022? Why?
【主流Nivida显卡深度学习/强化学习/AI算力汇总】
Proof of the thinking of Hanoi Tower problem
HiEngine:可媲美本地的云原生内存数据库引擎
COMSOL -- three-dimensional graphics random drawing -- rotation
codeforces每日5题(均1700)-第五天
Mongodb replica set
查看rancher中debug端口信息,并做IDEA Remote Jvm Debug
Web API configuration custom route
yolov5目标检测神经网络——损失函数计算原理
【yolov3损失函数】
【load dataset】
redis主从模式
谜语1
Solve readobjectstart: expect {or N, but found n, error found in 1 byte of
Dynamic SQL of ibatis


![[crawler] Charles unknown error](/img/82/c36b225d0502f67cd04225f39de145.png)




![[calculation of loss in yolov3]](/img/8c/1ad99b8fc1c5490f70dc81e1e5c27e.png)

