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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:45:19+00:00 2026-05-23T12:45:19+00:00

I have an application that periodically checks the server for some flag. And then

  • 0

I have an application that periodically checks the server for some flag.
And then displays a message depending on the value of this flag.

I don’t want to display a message then the application is not in a front.
And I use SharedPreferences to store the application state manually.
In each activity I do something like:

@Override
protected void onStart() {
    super.onStart();
    SharedPreferences.Editor prefs = context.getSharedPreferences("myprefs", getApplicationContext().MODE_PRIVATE).edit();
    prefs.putBoolean("appInFront", true);
    prefs.commit();
}
@Override
protected void onPause() {
    super.onPause();
    SharedPreferences.Editor prefs = context.getSharedPreferences("myprefs", getApplicationContext().MODE_PRIVATE).edit();
    prefs.putBoolean("appInFront", false);
    prefs.commit();
}

And this allows me to get a state of the application from “appInFront” preference:

SharedPreferences prefs = context.getSharedPreferences("myprefs", Context.MODE_PRIVATE);
boolean appInFront = prefs.getBoolean("appInFront", true);      

But may be exists native method or way to get a current state of the application (is the application in a front or is it in a background)?

  • 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-23T12:45:19+00:00Added an answer on May 23, 2026 at 12:45 pm

    What kind of message are you displaying?A Notification or something in your Activity?
    Where in your Application do you need that state information?

    You can write an BaseActivity and extend all other Activities from. So you need to write less code. And as counterpart of onPause() you should use onResume():

    public class BaseActivity{
    
    public static boolean appInFront;
    
    @Override
    protected void onResume() {
        super.onResume();
        appInFront = true;
    }
    @Override
    protected void onPause() {
        super.onPause();
        appInFront = false;
    }
    

    }

    With that static public boolean you can ask from “anywhere” for the visibility state of your app.
    You probably don’t need to remember the state between app restarts so a boolean is sufficient.

    if(BaseActivity.appInFront){
        //show message
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a web application that periodically sends out e-mails to users. At the
I have an application that displays an image inside of a Windows Forms PictureBox
C# - Winforms - SQL Server i am building an application which displays some
This application sends data periodically to a server. What I need to do is
We have an application that handling our own message - a timered procedure read
I have a application that periodically (via an NSTimer ) asks for the users
We have an ASP.NET application that uses SQL Server to store its session state.
As part of my web application I have a program that runs periodically during
I have an ASP.Net application that needs needs to have some work performed by
I have application that makes different queries with different results so the caching in

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.