How can I achieve the following start activity intent mode - android

I have a broadcast receiver launching a transparent activity while my application is either not launched at all or is in the background. This transparent activity behaves like a pop-up-box that appears on top of the android OS (thus transparent).
If my application is not launched at the moment (exists neither in foreground nor background), when the broadcast receiver triggers the start intent action everything behaves as it should and my transparent activity appears on top of the android OS.
I have another scenario where my application is paused - is in the background. From this state, if my broadcast receiver is triggering the action to start the transparent activity it does so in a way that it moves my last activity on the screen and my 'transparent activity' on top of it thus not being transparent anymore, I can see through it the contents of the activity behind it (from my application). This behavior is not desired and I wan't to change it in a way that when the pop up activity is started, the activity that was before be invisible. How can I do this, how can I hide all other activities from my app in the stack and have only the last one visible !?
This is how I send the intent:
Intent intent = new Intent(getApplicationContext(), PopTestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

use this
Intent intent = new Intent(getApplicationContext(), PopTestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Related

android Prevent a background app from launching activities in the foreground

I have an activity in my application that does a process and when finished, launches an activity with a normal intent:
Intent intent2 = new Intent();
intent2.setClass(anActivity.this, JustAnotherActivity.class);
startActivity(intent2);
finish();
The problem is that when you are doing the process, if I press the home button and take it to the background, when it finishes and calls the new activity, even if it is in the home of the phone, this new activity jumps to the foreground.
I am looking for the activity to be launched, but if the user is outside the application, it does not come to the foreground.
I guess the use of a FLAG is necessary, but the ones I have tried have not worked.
sorry for my bad english
Thanks.
what if you take care of ActivityLifecycle (OnPause, OnStop).
You can add a flag when the activity goes "OnPause/OnStop" depending what you want. And them perform the intent just if the activity is not "Pause/Stop".
Activity Lifecycle

Start pending intent instead of main activity, on app icon click

I have a foreground service on my application. When service running, notification will be display with a pending intent(this pending intent is not my main activity). When click on the notification, pending intent will be starts. Its working fine.
Following are my activities
Main activity - LoginActivity.java
Pending intent activity(which displays when click on notification) - HomeActivity.Java
When click on app icon while service is running, I need to launch pending intent activity(HomeActivity.java) instead of main activity(LoginActivity.java)
How could I do that?
You cannot change what the launcher icon points to dynamically at runtime in a reliable fashion.
You are welcome to have it point to an activity set up with Theme.NoDisplay, which then determines what actual activity should display, starts that activity using startActivity(), and then calls finish() to get rid of itself.
Or, have the launcher icon always point to HomeActivity, which has the logic to detect that a login is needed and then starts LoginActivity.

How do I get my existing Activity to display?

How do I get my app to appear in on the screen after it has been replaced by some other screen/activity? Some network event occurs and my application wants to reappear in the foreground (presumably in a polite manner).
I think I need to do something like:
contxt.startActivity(myActivity);
I don't want to create another instance of my app or cause it to restart, but I want it to appear.
Use FLAG_ACTIVITY_NEW_TASK
Intent intent = new Intent(contxt, myActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
contxt.startActivity(intent);
In your myActivity onNewIntent is called. I assume myActivity is the top activity in your app current stack

Activity not being reused

I have an odd issue with my application.
I start the application and the activity shows ok.
I then press the home key so that the activity goes into the background. I can see the onPause() method being called.
The application's service then creates a notification which shows on the status bar.
I then click on the notification and the activity is shown and I see that the onResume() method is called.
I then press the home key and the activity goes into the background. I can see the onPause() method being called.
If I now start the application by clicking on the applications icon I see that a new instance of the activity is created rather than using the paused instance.
If I press the home key again the new activity goes into the background.
Starting the application by clicking on the applications icon I see another new instance of the activity is created.
Pressing the back button at the point destroys each activity in return.
What I want to happen is that a single instance of the activity be used.
Any ideas?
Just use the same intent filters as android uses when launches the app:
final Intent notificationIntent = new Intent(context, MessageListActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
As the intent you created to open your activity from notification bar is the same as android used for launching your app, the previously opened activity will be shown instead of creating a new one.
You should look at the different launch modes for Android activities. this should help. Launch modes can be set in the androidmanifest.xml file. I think your solution would be to use the 'singleTop' launch mode.

Android: How to give focus to a background Activity belonging to a different task?

Say that the user has started my App and then switched off to use the Browser (so we have 2 sets of Tasks running). After a while, something happens to my app that requires the user's attention, so it posts a notification to notify the user. Is there a way to bring my App's task (and the Activity on top of the stack) out from the background when user clicks on my notification?
In your notification you call an intent to start your app, if you ensure it has the following flags then it will be brought to front rather than creating another instance:
Intent contentIntent = new Intent(this, ABC.class);
contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | INTENT.FLAG_ACTIVITY_SINGLE_TOP);
A notification usually comes with a PendingIntent (Notification.contentIntent). Set this intent to your app to start an activity of your app.
There is only one Activity that is currently active - as soon as you switch to a different screen, the system will pause your activity (onPause, and very likely onDestroy), so you'll need to use an intent to bring resume your activity and bring it to the main screen.

Categories

Resources