I posted a question yesterday as I have some specific requirements that seem to be quite difficult to achieve with android.
I am working on an app that shows sensitive data and a requirement is that whenever another app gets focus or the app goes into the background because the home button was pressed the app should navigate to the login page.
The app should also go to the login page when the 'all open apps' (not sure of name) button is pressed, as this should not show a screen shot of the current open activity but should show the login activity.
I have been playing around with variations of the code below:
@Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
if(level >= TRIM_MEMORY_UI_HIDDEN && ((!isInputtingPIN && !isLoggedIn) || isLoggedIn)) {
Log.d("AppGlobalData:", "*************** APP WINDOW IS BEING OBSCURED ***************");
Intent login = new Intent(this.currentContext, AppEntryPoint.class);
startActivity(login);
}
}
This works but the problem is that when the home button is pressed, the app goes into the background, but this firing of the new intent pulls the app back into the foreground. This also happens in the 'all apps' view.
If I defer this intent firing until the app comes back into the foreground this is fine but the app does not move to the login page until it comes back to focus, that means if the app was on a page showing sensitive data and the all apps button is pressed the view of the app shows sensitive data.
This was achieved in IOS by simply implementing a single method...
Can anybody tell me how I can get the intent to change the activity but keep the app in the background?
I managed to get everything working for my use case, in quite a simplistic way, this might prove useful for anyone wanting to do this in the future, I am not using onTrimMemory() anymore:
1) When a user has logged in I set a global variable isLoggedIn, I have a variable that is set whenever I trigged a new intent to another screen within my application:
public class GlobalData extends Application
public static boolean inTransition = false;
public static boolean isLoggedIn = false;
2) Created 2 methods to be implemented within each activities onPause() and onResume():
public static void onResumeHandler(Context context) {
inTransition = false;
if(!isLoggedIn) {
Intent login = new Intent(context, AppEntryPoint.class);
context.startActivity(login);
}
}
public static void onPauseHandler() {
if(!inTransition) {
isLoggedIn = false;
}
}
3) Each time I trigger a new intent in my application set the transition indicator variable:
case R.id.map_settings:
GlobalData.inTransition = true;
intent = new Intent(this, MapPreference.class);
this.startActivity(intent);
break;
4) In the activities call the handlers:
@Override
public void onResume() {
GlobalData.onResumeHandler(this);
super.onResume();
}
@Override
public void onPause() {
GlobalData.onPauseHandler();
super.onPause();
}
5) In preference activities I also had to handle onBackPressed() because in those instances I do not trigger a new intent to move away from the preference screen but instead the back button is pressed:
@Override
public void onBackPressed() {
GlobalData.inTransition = true;
super.onBackPressed();
}
From the small amount of testing I have done this seems to handle a press of the home button and a press of the 'All running apps' button and forwards to the login screen.
6) The final thing I have done, specific to my use case is that I did not want the 'All running apps' button to show a screen shot of the screen when it was pressed so in each activity I have added the following line to the onCreate() method to disable screen shots:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,WindowManager.LayoutParams.FLAG_SECURE);