I'm writing an app that requires an access code when starting the app. I also request this access code when retrieving the app from the background. To do that I use the event "onResume". Unfortunately "onResume" is also triggered when I rotate the screen. But I do not want to miss the rotation of the screen.
I followed the solution from Martin Marconcini: How to detect when an Android app goes to the background and come back to the foreground. But this did not help because "onMoveToBackground" also gets triggered on screen rotation.
I tried saving the orientation in onSaveInstanceState and load it in onRestoreInstanceState. Now if the Orientation not changed i locked the app. If the orientation changed i disabled lock. Now the problem is, that when the app is locked, you only have to rotate the screen and the lock is gone.
Update 1: These are snippets from my mainactivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_passcode);
initializeStartData();
if (appStorage.getSetting(AppStorage.SETTING_PASSCODE)) {
appUnlocked = false;
loadPage(LAYOUT_ID_CHECK_PASSCODE); //which contains setContentView(R.layout.activity_check_passcode);
} else {
appUnlocked = true;
loadPage(LAYOUT_ID_OVERVIEW); //which contains setContentView(R.layout.activity_overview);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(OUTSTATE_LAYOUT_ID, layoutId);
outState.putInt(OUTSTATE_ORIENTATION, getResources().getConfiguration().orientation);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState.getInt(OUTSTATE_ORIENTATION) != getResources().getConfiguration().orientation) {
rotated = true;
} else {
rotated = false;
}
layoutId = savedInstanceState.getInt(OUTSTATE_LAYOUT_ID);
loadPage(layoutId);
}
}
@Override
protected void onResume() {
super.onResume();
if (appStorage.getSetting(AppStorage.SETTING_PASSCODE) && !rotated) {
loadPage(LAYOUT_ID_CHECK_PASSCODE);
}
rotated = false;
}
As i said above, this code is nearly doing what i want:
- Starting the app shows the passcode page.
- Get the app back in the foreground (for example after pressing the home button) shows the passcode page.
- Rotating the screen with app in foreground does no show the passcode page
The Problem:
- App is in foreground, screen orientation is portrait
- Put app in background by switching the app or pressing the home-button
- Rotate the screen (smartphone) to landscape
- Get the app back in the foreground (Will not show the passcode page because the rotation changed)
Update 2: (The Solution)
As described here: Solution I found the solution. This is my working code:
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean(OUTSTATE_LOCKED, appUnlocked);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
appUnlocked = savedInstanceState.getBoolean(OUTSTATE_LOCKED);
}
@Override
protected void onStop() {
super.onStop();
if (isChangingConfigurations()) {
Toast.makeText(this, "Only screen rotation", Toast.LENGTH_SHORT).show();
} else {
setLocked();
Toast.makeText(this, "Closed app, Locking", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onResume() {
super.onResume();
if (appStorage.getSetting(AppStorage.SETTING_PASSCODE) && !appUnlocked) {
passcodeUnlock = "";
loadPage(LAYOUT_ID_CHECK_PASSCODE);
}
}