if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && activity.isDestroyed()) {
throw new IllegalArgumentException("You cannot start a observe for a destroyed activity");
}
if (!(Looper.myLooper() == Looper.getMainLooper())) {
throw new IllegalArgumentException("You must start a observe on mainThread");
}
fragmentManager = activity.getFragmentManager();
SupportActLifeListenerFragment supportFragment = findFragment(fragmentManager);//找到绑定的Fragment
LifeListenerManager manager = findLifeListenerManager(supportFragment);//找到指定Fragment的ActLifeListenerManager
manager.addLifeListener(Listener);//添加监听
原理就是在传入的Activity中加入一个fragment 这个fragment我们提前做好了生命周期的回调。
private LifeListenerManagerlistenerManager;
public void setActLifeListenerManager(LifeListenerManager manager) {
listenerManager = manager;
}
public LifeListenerManager getLifeListenerManager() {
return listenerManager;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public void LifeRequestPermissions(final String[] array,final int code) {
if (!isAdded()) {
Handler handler =new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && isAdded()) {
SupportActLifeListenerFragment.this.requestPermissions(array,code);
}
}
},500);
}else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && isAdded()) {
SupportActLifeListenerFragment.this.requestPermissions(array, code);
}
}
}
public void LifeStartActivityResult(final Intent intent,final int code) {
if (!isAdded()) {
Handler handler =new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
SupportActLifeListenerFragment.this.startActivityForResult(intent,code);
}
},500);
}else {
if (isAdded()) {
SupportActLifeListenerFragment.this.startActivityForResult(intent, code);
}
}
}
@Override
public void onStart() {
super.onStart();
if (getLifeListenerManager() !=null)
getLifeListenerManager().onStart();
}
@Override
public void onResume() {
super.onResume();
if (getLifeListenerManager() !=null)
getLifeListenerManager().onResume();
}
@Override
public void onPause() {
super.onPause();
if (getLifeListenerManager() !=null)
getLifeListenerManager().onPause();
}
@Override
public void onStop() {
super.onStop();
if (getLifeListenerManager() !=null)
getLifeListenerManager().onStop();
}
@Override
public void onDestroy() {
super.onDestroy();
if (getLifeListenerManager() !=null)
getLifeListenerManager().onDestroy();
setActLifeListenerManager(null);
}
@Override
public void onRequestPermissionsResult(int requestCode,@NonNull String[] permissions,@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (getLifeListenerManager() !=null)
getLifeListenerManager().onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
public void onActivityResult(int requestCode,int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (getLifeListenerManager() !=null)
getLifeListenerManager().onActivityResult(requestCode, resultCode, data);
}
参考代码github地址
觉得好就点个星星吧