Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6072361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:09:06+00:00 2026-05-23T10:09:06+00:00

I have an Android application that consists of several activities that are available to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:09:06+00:00Added an answer on May 23, 2026 at 10:09 am

    After further reading, it’s apparently not possible to receive notification of the Intent.CATEGORY_HOME intent, short of creating a custom home for the device, which was not the question here.

    To provide the desired behavior, I ended up doing something a little shakier than I’d hoped – but it is a reasonable enough approach and it has been working. The summary of the approach is to have every activity update a singleton timestamp onPause, and then in the onResume of each activity, check how long it’s been since that pause was set – if the difference is larger than some small amount, it means that the activity is being resumed from somewhere other than the app, and the PIN screen should be shown. A custom extension of the Application class provides an easy mechanism for managing the timestamp singleton and centralizing the checking code. Here is the code:

    public class MyApplication extends Application {
    
    private long mLastPause;
    
    public long getLastPause() {
        return mLastPause;
    }
    
    public void setLastPause() {
        mLastPause = new Date().getTime();
    }
    
    public boolean isAppResuming() {
        long now = new Date().getTime();
        long millisecondsSinceLastPause = now - getLastPause(); 
        return millisecondsSinceLastPause > 2000;
    }
    
    public Intent onResume() {
        if ( shouldShowPINEntry() == true ) {
            return new Intent( this, PINEnterActivity.class );
        }
        return null;
    }
    
    public boolean shouldShowPINEntry() {
    
        if ( isAppResuming() == true || Session.isActive( this ) == false ) {
            return true;
        }
        else {
            Session.extend();
        }
    
        return false;
    }
    }
    

    [the Session object in the code above is a separate singleton that determines whether the user has been inactive for too long. See this post for more on that pattern.]

    All my activities then extend a common SecureActivity, which interacts with the MyApplication like so:

    public class SecureActivity extends Activity {
        @Override
        protected void onResume() {
            Intent resumeIntent = ( (MyApplication) this.getApplicationContext() ).onResume();
            if ( resumeIntent != null ) {
                    startActivity( resumeIntent );
            }
            super.onResume();
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            ( (MyApplication) this.getApplicationContext() ).setLastPause();
        }
    }
    

    I’m still interested if anyone else has a solid solution that doesn’t rely on this “small interval between pause and resume”, but this will do fine for now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an android application that requres a login based on users in a
I'm working on an Android project that consists of several different Activities all associated
I have an Android application that writes data to the SD card. I want
I have an android application that is running. After a while when user quits
I have an android application that consists of a WebWiew and I need to
i have created application in android that consist of 5 to 6 activities. its
I have an Android application that contains two Activities . Activity A has a
I have an Android Application that has four activities. None is very large and
I have an android application that I want to always be running in landscape
I have an android application that has a button which I want to have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.