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

  • SEARCH
  • Home
  • 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 6713733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:24:53+00:00 2026-05-26T08:24:53+00:00

I have to maintain statistics based on session in my application. Problem is that

  • 0

I have to maintain statistics based on session in my application. Problem is that android never closes the application even when the user exits the application using the back button (might be because I’m running services in the background).

I would like to reset my session information if the application is in the background for say X minutes (to differentiate it from say a phone call or alarm). Is there any way to do that?

Maybe a some sort of a broadcast receiver to detect the event … ?

Let me try to explain the scenario a little bit. There is this part of my application which can be accessed only after authentication (entering a password). Now once the user is inside this part, I wouldn’t want to keep asking the user for authentication every time the activity is paused. But say the user presses the Home key and returns to the application after say, 20 minutes, I would like to ask for authentication.

EDIT : I am working with multiple activities, so I cannot detect the application going to the background simply setting a flag in the onPause().

  • 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-26T08:24:54+00:00Added an answer on May 26, 2026 at 8:24 am
    private static boolean isApplicationGoingToBackground(final Context context) {
    
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List<RunningTaskInfo> tasks = am.getRunningTasks(1);
            if (!tasks.isEmpty()) {
                ComponentName topActivity = tasks.get(0).topActivity;
                if (!topActivity.getPackageName().equals(context.getPackageName())) {
                    return true;
                }
            }
    
            return false;
        }
    

    Call in onStop. onStop gets called after onStart of whatever takes over the screen – if its an activity in the same apk package then you’re not going into the background. This does require the GET_TASKS permission, and if an activity comes up without you backing out it’ll still trigger (user selects something from the notification bar or something).

    Or bind to a service onStart & unbind onStop – the service will then be onDestroyed when all your activities are stopped (or track binds vs unbinds if you don’t want to rely on onDestroyed getting called – because it might not..).

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

Sidebar

Related Questions

I am developing an Android application where I have to maintain session. There are
I have to maintain an application that has a lot of columns that are
I am working on a web application by using ASP.NET C#. I have maintain
I have to maintain a server-side script written in JScript (NOT Javascript) that needs
I have a large classic ASP app that I have to maintain, and I
I have to maintain a variable in my Application.(cfm|cfc) to set the environment which
I have to maintain the number of user visits for each module of my
I have to maintain an old asp.net application, and need to disable all access
I have to maintain a large C++ mixed-mode application (VC++ 2005, CLR-support: /clr:oldsyntax). I
In some code that I have to maintain, I have seen a format specifier

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.