温馨提示×

android setMaxLifecycle的示例代码有哪些

小樊
87
2024-08-11 18:51:34
栏目: 编程语言

  1. 设置Activity的最大生命周期为onStop:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getLifecycle().setMaxLifecycle(this, Lifecycle.State.CREATED);
}
  1. 设置Fragment的最大生命周期为onPause:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    getLifecycle().setMaxLifecycle(this, Lifecycle.State.STARTED);
}
  1. 设置Service的最大生命周期为onDestroy:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    getLifecycle().setMaxLifecycle(this, Lifecycle.State.STARTED);
}

0