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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:46:41+00:00 2026-06-05T23:46:41+00:00

I have a Toast that is called from inside a Service . It always

  • 0

I have a Toast that is called from inside a Service.
It always appears regardless of the state of the app, as designed.

How can make it appear only when my app is in the foreground, and not when other apps are in front? That is, without moving it to Activity code.

  • 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-06-05T23:46:43+00:00Added an answer on June 5, 2026 at 11:46 pm

    mparaz,

    There are a number of ways to do what you want very easily. Before I do that, I’d like to address a very important flaw in your question. An App is not an Activity, and an Activity is not an App. As such, an app cannot be in the foreground or background. That is reserved only for Activities.

    A quick example and then to you answer. Your Service is part of your Application. The application is loaded though the Activity is not. If your Application were not loaded, your Service could not run. I only emphasize this because it is an important philosophy in Android and understanding the correct use of these terms and the concepts that define them will make it so much easier for you in the future.

    A Simple Solution

    You may extend the Application object and hold a simple public flag in the class. Then you can set the flag to false anytime the activity goes to the background and true when it comes to the foreground (or vice versa, of course).

    Extending the Application:

    public class MyApplication extends Application
    {
        static public boolean uiInForeground = false;
    }
    

    Setting the flag: In your Activity…

    //This may be done in onStart(), onResume(), onCreate()... 
    //... whereever you decide it is important. 
    //Note: The functions here do not include parameters (that's Eclipse's job).
    public void onStart()
    {//Notice the static call (no instance needed)
        MyApplication.uiInForeground = true;
    }
    
    public void onPause()
    {
        MyApplication.uiInForeground = false;
    }
    

    In your Service (where you call the Toast)

        if (MyApplication.uiInForeground)
            Toast.makeText(someContext, myMsg).show();
    

    It’s really as simple as that. Oh yeah… Don’t forget to tell the manifest that you are extending the Application. For your Application declaration in AndroidManifest.xml

    <application android:name=".MyApplication" .... >
       <!-- All other components -->
    </application>
    

    Hope this helps,

    FuzzicalLogic

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

Sidebar

Related Questions

I am getting an Runtime Exception:Can't create handler inside thread that has not called
I have created a app that displays all the content from res/raw folder. I
I have an android application that displays a toast every hour and then every
I have a BroadcastReceiver that can successfully catch a broadcast, but if I try
I have an app that listens to incoming messages, and if the originating sender
I have a service B that sends a specific number of messages in a
I have a listview that is populated by a by objects from an Offer
I have a method that creates popups on tap, called onBalloonTap and i am
I have an onClickListener that should call a class that shows and image from
I'd like to make a app who changes me the airplane mode. I 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.