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 7078269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:31:35+00:00 2026-05-28T06:31:35+00:00

I hope it exists. I would like to store the time when the application

  • 0

I hope it exists.

I would like to store the time when the application loses focus and then check if it has lost focus for more than n-minutes to bring up a lock.

Seeing how an application is composed of activities, I think there will not be a direct equivalent. How would I be able to achieve similar results?

EDIT
I tried to extend the Application class to registerActivityLifecycleCallbacks() and realized I will not be able to use this approach because it is only available in API Level 14+

  • 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-28T06:31:36+00:00Added an answer on May 28, 2026 at 6:31 am

    Allow me to share how I made a backwards compatible solution.

    I had already implemented the locking of my app on launch if there was a passcode associated with the account. To be complete, I needed to handle situations where other applications (including the home activity) take over for n-minutes.

    I ended up making a BaseActivity that all my Activities extend.

    // DataOperations is a singleton class I have been using for other purposes.
    /* It is exists the entire run time of the app
       and knows which activity was last displayed on screen.
       This base class will set triggeredOnPause to true if the activity before
       "pausing" because of actions triggered within my activity.  Then when the
       activity is paused and triggeredOnPause is false, I know the application
       is losing focus.
    
       There are situations where an activity will start a different application 
       with an intent.  In these situations (very few of them) I went into those 
       activities and hard-coded these lines right before leaving my application
    
       DataOperations datao = DataOperations.sharedDataOperations();
       datao.lostFocusDate = new Date();
    */
    
    import java.util.Date;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.util.Log;
    
    public class BaseActivity extends Activity {
        public boolean triggeredOnPause;
    
        @Override 
        public void onResume(){
            super.onResume();
            DataOperations datao = DataOperations.sharedDataOperations();
            if (datao.lostFocusDate != null)    {
                Date now = new Date();
                long now_ms = now.getTime();
                long lost_focus_ms = datao.lostFocusDate.getTime();
                int minutesPassed = (int) (now_ms-lost_focus_ms)/(60000);
                if (minutesPassed >= 1) {
                    datao.displayLock();
                }
                        datao.lostFocusDate = null;
            }
            triggeredOnPause = false;
        }
    
        @Override
        public void onPause(){
            if (triggeredOnPause == false){
                DataOperations datao = DataOperations.sharedDataOperations();
                datao.lostFocusDate = new Date();
            }
            super.onPause();
        }
        @Override
        public void startActivity(Intent intent)
        {
            triggeredOnPause = true;
            super.startActivity(intent);
        }
        @Override
        public void startActivityForResult(Intent intent, int requestCode)  {
            triggeredOnPause = true;
            super.startActivityForResult(intent, requestCode);
        }
    
    }
    

    If you are going to use this solution and have trouble implementing the equivalent of my DataOperations class, please comment and I can post the necessary code.

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

Sidebar

Related Questions

Hope this is not a dupe. I would like to be able to do
I started similar thread but as the problem is more difficult, I would like
I would like to check used bandwidth when playing a video with MPMoviePlayerController to
Hope this isn't a waste of your time. I'm working on a project, and
I have a lot of code and would like to avoid posting it all
I would like to be able to spawn a process in python and have
I'm working on a school project in which we would like to analyze the
I am using jQuery Ui Autocomplete 1.8.14 and I would like to do two
I am writing a validation class in PHP which I would like to be
I know a topic already exists like that but I do not want to

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.