As what I can understand from your problem. You facing null of all objects in onResume()
which lead to the app crash.
And you cannot really avoid onResume()
being called again. It's expected behavior from activity lifecycle. But there's a trick. You can just create a flag to know if a screen was off/on in onPause()
. Once the phone is unlock, it will call onResume()
and you can manage that flag.
boolean isScreenUnLock = false;
@Override
protected void onPause() {
super.onPause();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
isScreenUnLock = pm.isScreenOn();
}
@Override
protected void onResume() {
super.onResume();
if(isScreenUnLock){
//Do something
}
}
But it seem not a better way. I would recommend to handle the activity state instead of avoid all objects in Activity null. Check this example for more detail.
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putBoolean("MyBoolean", true);
savedInstanceState.putDouble("myDouble", 1.9);
savedInstanceState.putInt("MyInt", 1);
savedInstanceState.putString("MyString", "Welcome back to Android");
// etc.
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
double myDouble = savedInstanceState.getDouble("myDouble");
int myInt = savedInstanceState.getInt("MyInt");
String myString = savedInstanceState.getString("MyString");
}
Or quick way to do handle state above. Just simple use this library.