I have two activities in my app.
The first one, which is login (the one which is started as Main/Launcher)
The second one, which contains fragments and so the main content. So Login activity starts Main activity.
When I receive a notification and click on it, I want to be able to handle those scenarios:
resume app if it's in background (like user resume it like pressing home key) but without showing the launcher activity (login).
launch app if it's not launched yet.
I tried with mode singleTask for both in Manifest and here the code in my service:
Intent intent = new Intent(this, MainActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Then I pass pendingIntent in the setContentIntent method of the notification.
But when I click on the notification, and the app is already launched and in background, it restarts the Login activity (I don't want it) but resume the Main activity correctly.
Related
I have a widget for my application. This application has two activity: The launcher and the main one.
On click on this widget, it opens the linked app, however for now I'm opening the launcher and then I start my main activity from its launcher activity even if it was already running on the background.
Is it possible, on click, to check if the main activity is already running on the background and if so, resuming it; or opening the launcher otherwise?
There is the code I'm using now to opening the launcher from the widget.
//LISTENER TO OPEN THE APP ON CLICK
Intent clickIntent = new Intent(context, SplashScreen.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, clickIntent, 0);
// Get the layout for the App Widget and attach an on-click listener to the button
views.setOnClickPendingIntent(R.id.widgetstationlayout, pendingIntent);
To bring your application to the foreground in whatever state it was left in, use this:
Intent intent = getPackageManager().
getLaunchIntentForPackage("your.package.name");
startActivity(intent);
If your app is already running, this will bring the existing task to the front in whatever state it was in when it was moved to the background. If your app is not running, this will launch your app from the beginning.
This behaves as if you clicked on the app icon.
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!
This is the code I use to create the PendingIntent for my notification.
Intent notificationIntent = new Intent(context, Activity1.class);
PendingIntent myIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
This PendingIntent launches Activity1 when the notification is click.
How can I simply reopen the app and go to the most recent Activity (as though clicking on the launcher icon) instead of launching a new Activity when the notification is clicked?
Activity1 is just an example. I have multiple Activity in the app. I just want to reopen the app and go to the most recent Activity
NOTE: this looks like wrong design for me, because notification should allow user to enter activity that is in context with the notification.
Technically, you can create redirecting activity and your notification intent should launch it when tapped. In its onCreate() you check what activity you want user to be redirected (you can keep this info in SharedPreferences, and each activity would write this info in onCreate() (or make that in your base class if you have it). Then in redirector you call regular startActivity() to go last activity and call finish() to conclude your redirector. Moreover, your redirector activity does not need any layout so add
android:theme="#android:style/Theme.NoDisplay"
to its Manifest entry (of course you also need no call to setContentView())
create Activity1 as a singletask activity , by changing the launchMode of the activity to singleTask...
set your activity to launchMode="singleTop" in your Manifest.xml then use this code instead of what you are using above to reopen the active one:
Title = "YourAppName";
Text = "open";
notificationIntent = new Intent(this, Activity1.class);
cIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), Title, Text, cIntent);
You can change the android:launchMode in the manifest file for the activity targeted by the pending intent.
Typically, you can use singleTop, which will reuse the same instance when the targeted activity is already on top of the task stack (i.e.: Activity is shown before you left your app).
You can also consider SingleTask and SingleInstance, if you want to keep only a single instance of the activity.
In my app, i created a custom notification that contain a button that show recent apps(instead of home button long press)
when user open main activity and press home button to go to home screen then drag the notification drawer and click on the button:
Desired result is that the recent apps are shown and the main activity is one of the recent apps.
Actual result is that the recent apps are shown but the main activity is not in them and the main activity resumes.
My code to start "RecentApps (is a dummy class that show recent apps)" class with pending intent
Intent recentAppIntent = new Intent(getBaseContext(), RecentApps.class);
PendingIntent pendingrecentAppIntent = PendingIntent.getActivity(getBaseContext(), 1, recentAppIntent, 0);
notificationView.setOnClickPendingIntent(R.id.recentAppButt, pendingrecentAppIntent);
I think that the problem is getBaseContext, so when the main activity is alive and not finished the recent apps are shown but with context is the main activity.
I tried getApplication and getApplicationContext but not working.
I also tried to use flags for "recentAppIntent" but it is not working, It is a half solution to use
recentAppIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
but it not what i need.
So, my question is "How to finish the main activity in which the pending intent starts".
Thanks in advance, Mostafa
You need to ensure that your MainActivity and your RecentApps don't belong to the same task. The "Recent apps" doesn't actually show recent apps. It shows recent tasks.
To make sure that your RecentApps isn't in the same task as your MainActivity, you can add the following to the <activity> definition in the manifest for RecentApps:
aandroid:taskAffinity=""
Also, when creating the notification, add FLAG_ACTIVITY_NEW_TASK to the Intent, like this:
Intent recentAppIntent = new Intent(getBaseContext(), RecentApps.class);
recentAppIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingrecentAppIntent = PendingIntent.getActivity(getBaseContext(), 1, recentAppIntent, 0);
notificationView.setOnClickPendingIntent(R.id.recentAppButt, pendingrecentAppIntent);
I think FLAG_ACTIVITY_CLEAR_TOP will help as a flag.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Explanation for Intent flags are available here: link
I am having problems launching my application from my widget when it's clicked upon.
Here is a cut of some code
// Create an Intent to launch activity
Intent intent = new Intent(context, Entry_MainTabView.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
updateViews.setOnClickPendingIntent(R.id.Widget, pendingIntent);
Now take the scenario.
I launch my app, exit to the home page , then click on my widget.
It launches a second instance of the application, so when I hit "back" , the application goes away and reveals the other copy. I then hit back again and finally go back to the homescreen.
How can I make the Intent only create a new instance if none already exists in ram ?
Have you looked at singleInstance and singleTask launch modes of an activity? This extract from Pro Android 2 indicates that there is no documented way of ensuring that there is only one instance of the widget running.