I have an Android application that consists of several activities that are available to the user behind a login acitivity. What I want to do is to show a PIN/login activity when the user hits the HOME button (from any of the activities) and then subsequently returns to the application. Effectively, what I'd like to do is to log out the user when the HOME button is clicked.
From reading other posts, it's my understanding that there is no way to handle the HOME button click directly using a key click event handler. I have tried adding an intent filter for the CATEGORY_HOME intent in the activities:
@Override
public void onCreate( Bundle savedInstanceState ) {
IntentFilter homeIntentFilter = new IntentFilter();
homeIntentFilter.addCategory( Intent.CATEGORY_HOME );
BroadcastReceiver homeReceiver = new BroadcastReceiver() {
@Override
public void onReceive( Context context, Intent intent ) {
showHomeToast();
}
};
registerReceiver( homeReceiver, homeIntentFilter);
}
public void showHomeToast() {
Toast homeToast = Toast.makeText( this, "home clicked", Toast.LENGTH_LONG );
homeToast.show();
}
But this doesn't seem to fire.
Additionally, since there are many activities in the application, triggering a logout in the onPause or onStop events seems problematic since they will fire when the user is simply switching to other activities in the application - and in that case, the user should not be logged out of the application. (also, from experimentation, onDestroy will not be invoked simply by hitting thing HOME button)
At this point, I feel like I'm fighting the Android architecture, so I'm looking for an alternate approach. I have contemplated trying to invert my approach and to use the onResume event, but again I'm unclear how to know whether a given activity is being resumed from another activity in the application (no PIN/login necessary) or because the user has invoked the application again.
Any approaches would be appreciated.