how to start activity when the main activity is running in background?

I created an application which enables the user to set whether he wants to receive notification while the application runs in background mode. If the notifications are enabled an activity should be started (the dialog should appear on the screen).

I tried to enabled it the following way:

public void onProductsResponse(List<Product> products) {
    this.products = products;
    if(moboolo.getAutomaticNotificationsMode() != 0 && products.size() > 0){
            Intent intent = new Intent(this, ProductListActivity.class);


this is the method from main activity. When onPause() is executed isRunningInBackground is set true. When I tried to debug it when the main application was running in the background the line

startActivity(intent) had no effect (the activity didn't appear).

Does anyone know how to midify the logic in order to start an activity from the main activity when the main activity is running in the background (after onPause() is called)?

Thank you.

Asked by: Dainton107 | Posted: 24-01-2022

Answer 1

You can't force an Activity to appear from an application running the background. The documentation says:

If the application is running in the background and needs the user's attention, the application should create a notificaiton that allows the user to respond at his or her convenience.

If your Activity is paused the user may be doing something else in a different application and probably doesn't want your Activity suddenly appearing on top of what they are currently doing.

You should be using a Status Bar Notification. This allows your application to put an icon in the Status Bar. The user can then slide down the Status Bar drawer and click on your notification to open your application and show the relevant Activity. This is the way the vast majority of Android apps notify the user when running in the background.

Answered by: Jared594 | Posted: 25-02-2022

Answer 2

To complete Hemendra's answer, you don't need any of those flags except FLAG_ACTIVITY_REORDER_TO_FRONT. You just need to create a PendingIntent from your normal intent and call the new PendingIntent's send() method to dispatch the intent. Here is how I did it:

Intent yourIntent = new Intent(this, YourActivity.class);
// You can send extra info (as a bundle/serializable) to your activity as you do 
// with a normal intent. This is not necessary of course.
yourIntent.putExtra("ExtraInfo", extraInfo); 
// The following flag is necessary, otherwise at least on some devices (verified on Samsung 
// Galaxy S3) your activity starts, but it starts in the background i.e. the user
// doesn't see the UI
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, 
                                                        yourIntent, 0);
try {
    pendingIntent.send(getApplicationContext(), 0, yourIntent);
} catch (Exception e) {
    Log.e(TAG, Arrays.toString(e.getStackTrace()));

Answered by: Lana634 | Posted: 25-02-2022

Answer 3

Intent i= new Intent("android.intent.category.LAUNCHER");
i.setClass(getApplicationContext(), MyActivity.class);
PendingIntent i2 = PendingIntent.getActivity(getApplicationContext(), 0, insIntent,Intent.FLAG_ACTIVITY_NEW_TASK);
try {
     i2.send(getApplicationContext(), 0, i);
} catch (Exception e) {

And on MyActivity's onCreate...


This will bring your activity to front event if main activity is running in background.

Answered by: Brianna858 | Posted: 25-02-2022

Similar questions

android - Activity from a background activity?

Am having an application contains activity A,B,C and A is launched from the browser and B is launched from A, a count timer running on A launches an activity C if the timer hits. Could any one plz tell the stack order for the activity of this application. either A->B->C or A->C->B. However now the visible activiy should be C if i press back key from C which should display either A or B ? Thanks in advance.

android - How to send data from a background thread created by one activity to another activity that is known to be active?

First, I should be clear by providing a little background: I'm using Eclipse to develop an Android application that plots Bluetooth data. It consists primarily of a Bluetooth Activity, a background thread created by the Bluetooth Activity, and a Plotting Activity. The primary focus of my question: Once the Plotting Activity is known to be active, what approach can I use to send BluetoothData from the background thr...

android - pass data to background activity without change the current activity

I have 2 activity. Activiy A will list links to download. Activity B has a listview of download item. When I click the link in activity A, how to send the link to activity B to download without change activity A (while activity B still downloading on background) ?

android - Activity kill after some time when i keep my Activity in background

I am stuck in a complex problem where my activity kill if i keep it in back ground for some times using method moveTaskToBack(true). when i again restart my application then activity's onCreate() method is called. my code is @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { moveTaskToBack(true); ...

java - how to change next activity background when i press button in current activity

i want to change the background for next activity when i press a button in my current activity &amp; i have 32 buttons and all will go to same activity but i want to change the back ground of it according to button prees so it will change 32 times I try this but it doesn't work buttons[0].setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-...

How to change Activity As background color from Activity B in Android?

In my app I'm trying to change my Activity's background color. For example from Activity B I should be able to change the background of Activity A, how can I do this? I tried findViewById, but app crashes. Any solutions? Thank you very much! P.S: I'm using AmbilWarna ColorPicker!

android - Start another activity in background on start of main activity

I have 2 activities Activity A and Activity B. Activity B start on click of button in Activity A. I want to start Activity B in background when the Activity A is created and move to Activity B when I click on button in Activity A. Please help me. Thanks

Keep Android Popup Windows always on top , even though activity goes in background and new activity comes back

This is a popup window which is being shown from an activity A. User presses button on Activity A then Activity B comes on top. But problem is popup also goes behind activity B. I want pop up to be always on top even though activity goes in back ground LayoutInflater layoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); LeadOffPopView = layoutInflater.inflate( R.layou...

java - How do I change the background color of an activity from another activity?

I have MainActivty and SettingsActivity, when I press a button in my SettingsActivity I want the background color of the MainActivity to change. How do I achieve this? Sorry if this question is a bit dumb, I recently started out and I'm a bit lost.

android - Update an activity by events of a background activity

I have 2 activities A and B. A has a recyclerView with list of songs. When i click one command goes to B and it starts playing it. B has an onCompletionListener() so it changes and plays song automatically. When one hovers back to activity A, name of currently playing song is displayed in a textView there. Problem is that when a song is is changed in background, the textView can't be automatically updated. One...

android - Why "Activity Screen" doesnot shown RECENT Apps while activity launch from background service in Redmi device

In my android application i start my service from background through push notification.When i receive push notification i am waking up my activity using below code: Intent it = new Intent(""); it.setComponent(new ComponentName(context.getPackageName(),IncomingActivity.class.getName())); it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.getApplicationContext().startActivity(it)...

android - How to show password activity immediately without existing activity getting flashed for few seconds while coming from background?

I want to add password screen always at top of stack when app opens from background. I am able to find out method when app comes from background using ActivityLifecycleCallbacks or LifecycleObserver and can show password screen in onStart of base activity. but for few seconds, user see existing activity from where it went to background and then intents to password activity. I ...

android - First Activity gets closed in background from Second activity

I am facing one issue and its a bit weird. My case is, I have two Activity A and B. A is my launcher activity so when i open app A is presented. Now from button click i start another activity B. Now i am placing my app in background by pressing home button of the phone. and coming again in the app from recent. Now i am in activity B when my app comes to foreground. Now when i press back button my applicatio...

Remove the Background Activity Task in Android which only Task Create But Activity Never Focus

In My App, When i am start creating New Activity-1 as singleInstance. So, it follow below Steps in Android Execution. (So, Stack look like TASK-A(10000099)-&gt; Activity-1) I am_create_task: [10,10000099] I am_create_activity: [10,225706045,10000099,...] So, before Task A: 1000099 complete the other process to create activity I am Launching Another New Ac...

android - Change background on an activity from another activity

I'm trying to change the background on an activity from another activity but it's not working. ConstraintLayout layout = findViewById(; layout.setBackgroundResource(R.drawable.summer); When I change the main_layout (main activity) to second_layout (second activity) it works on the second activity, but I can't get it to work on the first activity.

android - App always starts fresh from root activity instead of resuming background state (Known Bug)

I am facing exactly the problem mentioned in these links:

android - How can I bring an activity to the front from a background service

First of all, I am aware my issue is against the philosophy of Android, but I have no choice, this application will run on a embedded car gps and I need to bring an activity to prevent from car accident, for example, when it's happen around the user. I have to put other activity on the back and bring my alert pop up without user manipulation like notification on the front. Is there a way to bring manually an activi...

java - How can I update information in an Android Activity from a background Service

I am trying to create a simple Android application that has a ActivityList of information, when the application starts, I plan to start a Service that will be constantly calculating the data (it will be changing) and I want the ActivityList to be in sync with the data that the service is calculating for the life of the app. How can I set up my Activity to be listening to the Service? Is this the best way to approac...

android - how can I do something before current activity go background by Back key?

When back key is pressed, current activity goes background. I'd like to show a popup and let user choose really close this activity, before current activity go background. I tried to override onPause(), but it's called after activity goes back. Please somebody explains me how to do that?

android - How to set the background color of new activity after clicking tabs

I am switching activities on tab clicks and successful at this. But, in one of my Activity class I am doing the following: public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); } main.xml has the following: &lt;LinearLayout xmlns:android="" android:orientation...

android - Background tasks and activity

Has anyone any idea on how to solve the generic problems related to starting a task in background from an activity and when the task is finished posting the result to the activity that created ? (the activity might get destroyed in the meantime due to a orientation change ,or receiving a call , or might be in the process of destroying and recreation)

android - how to add background image to activity?

using theme or ImageView ?

Android: How to start an Activity in the background?

Is it possible to start an Activity in the background? I need such an Activity that can operate in the background and spawn other Activities as needed in response to user commands issued to my Home Screen widget.

Android: How to detect if an Activity goes into the background?

I need a way for my Activity to known that it's going into the background, and not when it's getting killed. I thought about hooking at onStop(), but onStop() is called for both cases. In other words, is there a way to tell if when my Activity is moving into the background and not getting killed?

android: How can I send an activity to background & call her from there

I am in process of creating some test application "Time Tracker" app like stopwatch so I start app go to Activity where I start time counter and now I need some functionality to send my activity to background or minimize or hide and after some period to call this activity again and stop her counter to get passed time period . How can I send Activity to back and call her to front but not to stop timer c...

android - How to avoid the ugly "boxes" when child views in a View has another color than the background (stateful drawables for example)

I have a simple ListView and on that ListView I have placed a number of custom defined Views. The CustomView has ImageView and two TextViews. The CustomView also has a "stateful drawable" as background, so that the background image (a 9-patch) changes if you press the Row in the ListView. When pressing the Row, the background image changes to a Red-ish thing. The problem is that when the background ...

Android drawing - Background caching

Quick question - I'm having control that's extending LinearLayout and I'm overriding it's onPaint method like this @Override protected void onDraw(Canvas canvas) { super.dispatchDraw(canvas); _background.draw(canvas); _object1.draw(canvas); _object2.draw(canvas); _object3.draw(canvas); // etc... } Every 40ms I invoke postInvalidate() in background and onPaint gets called on...

android - View on press onpress: Change background color on press? How do I show that the View is being pressed?

I have, for the time being, a custom view with a 9-patch image as a border. That custom view is placed three times in a LinearLayout, so it looks like this: +------------------------+ | CustomView | +------------------------+ | CustomView | +------------------------+ | CustomView | +------------------------+ I have attached a click event listener...

android - how to structure my app to run in background

I am new to Android, and I need some advices for start up. I want to build an application, which will show up, when the user gets into some hot situation. By hot situation I mean: the GPS/cell coordinates are in known zone; known Bluetooth device detected; known Wi-Fi network detected; weather info has change; I see something running in backgroun...

audio - Android - how to do background threading properly?

Was wondering if anyone could help me on background threading on Android. I have a piece of code that records from the mic of the device and then plays back what it records through the ear piece(on 1.5). I am trying to run it in a thread but have been unsuccessful in getting it to run as a background thread. Currently it runs and locks up the activity so that all thats happening is the thread is ru...

android - How to set background color of a View

I'm trying to set the background color of a View (in this case a Button). I use this code: // set the background to green v.setBackgroundColor(0x0000FF00 ); v.invalidate(); It causes the Button to disappear from the screen. What am I doing wrong, and what is the correct way to change the background color on any View? Thanks.

Android Widget Text Background

I have an app with a widget but I am having some difficulty with the layout of the widget. The basic idea if the widget should look like an icon and have a little text tag under it like any other icon on the desktop. I found one example which uses an android:background for the TextView and uses a drawable XML: &lt;shape xmlns:android=""&gt; &lt;solid...

How do I change the background of an Android tab widget?

My class extends extends TabActivity TabHost mTabHost = getTabHost(); TabHost.TabSpec tab1 =mTabHost.newTabSpec("tab1"); TabHost.TabSpec tab2 =mTabHost.newTabSpec("tab2"); tab1 .setIndicator("title tab1"); tab2 .setIndicator("title tab2"); mTabHost.addTab(tab1);mTabHost.addTab(tab2); TabHost.setCurrentTab(0 or 1) Can anybody guide me how do I change the background image or color of sel...

android - Set title background color

In my android application I want the standard/basic title bar to change color. To change the text color you have setTitleColor(int color), is there a way to change the background color of the bar?

Run a service in the background forever..? Android

I am doing a Battery Consuming research on the Android phone. I want to run a Battery Check every 10 min till the battery totally dies. I have been having problems to make it work. At my first try, I use a timer in a service class, and schedule the battery check every 10 mins. But soon I found that the service got paused when the screen goes off. Then I try to use AlarmService, I use a alarm call to wake my...

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android