Open activity from notification Jelly Bean issue - android

Just to make it short:
what I need is open an activity, start a countdown, minimize, launch a notification when timer goes to 0 and by tapping the notification go back to the previous state of the activity without creating a new one
what I have is this:
Intent intent = new Intent(ctx, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(ctx, 0, intent, 0);
It works perfectly for android <4.2 but when runs on Jelly Bean, it opens a new instance of the Activity.
It seems that Jelly Bean does not recognize none of the flags Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP
How can I make it run for 4.2+ ?

Just set your activity launch mode as
launchMode="singleTop"

Related

How to resume app from background like tapping app launcher icon?

I'm developing a sticker keyboard for Android. In the keyboard layout, I have a button. When I minimize the app then open the keyboard and I tap the button, I want to open my app on the latest screen.
There are steps:
Open app (LauncherActivity)
Go to Activity 1 (TheFirstActivity)
Go to Activity 2 (TheSecondActivity)
Minimize the app
Open another app and open my keyboard
Tap the button.
When I tap the button, I want my app to be opened at the Activity 2 and the app state is restored like the state when the app is minimized.
This is my code:
Intent intent = new Intent(this, LauncherActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Use this
Intent intent = new Intent(this, yourActivity.class);
// set the new task and clear flags
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
use this code for this
Intent intent = new Intent(this, LauncherActivity.class);
// set the new task, clear flags and From History flag
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
startActivity(intent);
I'll take this scenario :
I assume you have a background service from where you want to resume your activity in a paused state in the background
and also, you just want to resume the activity with all his previous state as the system does when your app is on pause or like it's done when the notification is clicked.
For that case, you can use the code below
public void resumeActivityFromBackground(Intent intent, boolean restart) {
if (intent == null)
intent = new Intent(BackgroundServiceClass.this, MainActivity.class);
intent.putExtra("activity_started_from_service", true);
intent.setFlags(
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
| Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP
/*| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED*/);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
//PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
startActivity(intent);
stopSelf(); //Use this to stop the service if you are launching from a background service.
}
Intent.FLAG_ACTIVITY_NEW_TASK - Will is what helps to bring the existing activity from background without recreating a new instance.
Intent.FLAG_ACTIVITY_CLEAR_TOP - Will make sure that the activity your are resuming is actually the one on top
This is what worked for me.

Activity flags / launchmode / pendingintent

i read a lot on the android documentation about launchmode and activity flags but i can't figgure it out.
i'm using the following code to open my activity from nothification:
Intent intent = new Intent(context, home.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, NOTIFICATION_ID++, intent, PendingIntent.FLAG_UPDATE_CURRENT);
in the manifest i declared my home activity as
android:launchMode="singleInstance"
all works fine except for when i close my app (not only in background) only the
onstart method gets called but not the oncreate and i have no idea why?!
i need the following:
app is open and visible to the user: user click on nothification, the existing HOME activity get active and the onnewintent gets calls (works fine)
app is in background (user pressed home button):: user click on nothification, the existing HOME activity get active and the onnewintent gets calls (works fine)
app is closed: user click on nothification, home activity gets created because its the first run! (does not work)
thanks for any help!

Activity with Intent.FLAG_ACTIVITY_NEW_TASK has been destroyed

I'm using this way to start activity from Service, if my app is background:
Intent intent = new Intent(this, FromBackgroundActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
it works perfect,
but if FromBackgroundActivity is active and I press Home key it puts all to background.
And if I click on my app in taskbar it restores my app to foreground but without FromBackgroundActivity, it is destroyed. (
how can i restore my app from background with FromBackgroundActivity?
Try intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
It'll clear all the task and go to the FromBackgroundActivity.

android click notification bring back the application

I want that when the user click on a notification, it brings back the app (instead of create a new instance).
Here is my code (monodroid : in c# but even if you know how to do in android, you may help me).
Intent i = new Intent(context, typeof(MainListProductActivity));
i.AddFlags(ActivityFlags.NewTask | ActivityFlags.BroughtToFront | ActivityFlags.ReorderToFront); // You need this if starting the activity from a service
i.SetAction(Intent.ActionMain);
i.AddCategory(Intent.CategoryLauncher);
context.StartActivity(i);
It works, but it bring back me to the MainListProductActivity. I want that the notification bring back me to the last viewed activity. How to do that ?
More : if the app is not started, I want that the app is launched with the default activity (splashscreen for me) with a parameter.
Thanks.
I solve that by launching a DummyActivity which call Finish on OnCreate.
It works well, even if is not the best way to solve that.
DummyActivity is a great solution. I use it too. Just create dummy and put it instead a needed class on Intent creation:
Intent intent = new Intent(this,DummyActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
This problem was solved for me for Xamarin by using:
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ResetTaskIfNeeded);

Android Activity singleton

I have an activity called MainActivity. This activity launches a notification that has a PendingIntent that opens this MainActivity.
So, to close the application, I have to click the back button twice. I would like to set up activity as singleton. I tried to set singleInstance or singleTask to manifest but this doesn't work.
singleInstance and singleTask are not recommended for general use.
Try:
android:launchMode="singleTop"
For more information please refer to launchMode section of the Activity element documentation.
In addition to the previous reference you should also read tasks and back stack
If you need to return to your app without creating a new instance of your activity, you can use the same intent filters as android uses when launching the app:
final Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
As the intent you created to open your activity from the 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.

Categories

Resources