I am experiencing a weird behavior on my android application. When I open my application, I see my DashboardActivity, then I hit home button or back button and my application closes. This is ok. Then I receive a push message and with this push message I create a notification. The notification works fine, I click the notification and it opens my activity, using the code below:
Intent notificationIntent = new Intent(context, BookingOfferActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle b = new Bundle();
b.putSerializable("booking", booking);
notificationIntent.putExtras(b);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Then I execute some task in this BookingOfferActivity activity and call the method finish() to make sure this activity will be finished no matter what. Then I open my application again, but instead of seeing the DashboardActivity I am still seeing BookingOfferActivity.
I have tried the solution proposed here:
Prevent new activity instance after clicking on notification
but it just doesnt work.
Is there a way to force my application to always open on the DashboardActivity?
Thanks
T
That is strange behaviour considering you are calling finish()
Try setting
android:noHistory="true"
android:launchMode="singleInstance"
in the manifest for the BookingOfferActivity
i don't know exactly, but u can try to finish all activities in onStop() method.
in onResume() method start your DashboardActivity.
Try removing SINGLE_TOP from your intent. CLEAR_TOP should be all you want.
From the Android Developer documentation
FLAG_ACTIVITY_SINGLE_TOP -> If set, the activity will not be launched if
it is already running at the top of the history stack.
Related
I have problem with notifications. It all works well launching correct activity from notification.
Problem is that when new activity is launched from notification and app has been in background. Then first the latest activity is loaded (on Resume()) and after that intended activity is loaded. In onResume I'm checking the session expiration. And when its expired user will be taken to login. And in the latest activity i don't have notification extras.
Is there possibility to skip latest activity onResume when opening notification and going straight to the intended activity.
PendingIntent createContentIntent(Context appContext, NotificationData data) {
Intent notificationIntent = createNotificationIntent(appContext, data);
if (notificationIntent == null) {
notificationIntent = new Intent();
}
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle extras = createBundle();
extras.putSerializable(EXTRA_NOTIFICATION_DATA, data);
notificationIntent.putExtras(extras);
return createPendingIntentActivity(appContext, notificationIntent);
}
Modify your new activity manifest to include custom task affinity, and consider changing launchMode to singleTop to prevent creating duplicates of the activity:
<activity
android:name=".your_activity"
android:launchMode="singleTop"
android:taskAffinity=":task_2"/>
Also include Intent.FLAG_ACTIVITY_NEW_TASK in your launch intent.
The problem you are having is that Android is bringing your existing task to the foreground and then (obviously) resuming the top Activity in the task BEFORE it creates the Activity from the Notification. This sounds like an Android bug.
Anyway, to get around this, you could try having the Notification launch an Activity into a different task. To do this, set taskAffinity="" on the Activity that the Notification starts. The Activity started by the Notification would then have to start another Activity that will run in the task with the rest of your app. Hopefully this would all happen after the phone is unlocked so that you don't see this onResume() problem.
If you need more help, please post the code from createNotificationIntent() and createPendingIntentActivity() and the relevant parts of your manifest.
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.
I'm developing an android application, and in one activity, when the user presses the HOME button, a notification appears. Upon pressing the notification the activity would resume from where it left off. The problem is that the activity is recreated each time. Upon pressing the notification onDestroy() and then onCreate() is called. But say if I reopen the activity through the "Recent apps" it resumes successfully.
This question has been asked before on stackoverflow and I have tried the answers provided and none of them seem to work. The API that I'm using is the latest 4.4. The options I have tried are:
Playing around with the Intent flags
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
Setting the activity to singleInstance and singleTask
How can I mimic the intent that is sent when I open my activity through 'Recent apps'?
EDIT: One more caveat that might be important. There's 3 activities in my program A B C. When clicking on the launcher icon activity A is first started. The notification is created only when going from activity C to Home. I'm trying to resume activity C.
Problem solved.
I was debugging the application through LogCat and looking at the intents that were being generated by the ActivityManager. Whenever I was changing the flags of the Intent, I wouldn't see the change in LogCat. So
02-12 11:26:39.586: I/ActivityManager(766): START u0 {flg=0x1000c000 cmp=com.example.app/.controllers.Activity3 bnds=[0,153][1080,345]} from pid -1
Changing the Intent flags didn't change the flg variable in the log. Apart from this, the notification image was staying in the Status bar, my app wasn't destroying it. What I did was add the PendingIntent.FLAG_CANCEL_CURRENT flag to the PendingIntent I was sending:
Intent intent = new Intent(this, Activity3.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
mBuilder.setContentIntent(pendingIntent);
So the problem was an incorrect PendingIntent that I must have created before, that was never being removed. Now I'm able to resume the activity by clicking the notification. Hope this helps someone that might have a similar issue.
guess I'm having some misunderstandigs with the Intent Flags.. What I'm trying to do is, I'm having a radio streaming applications, which has two Activities (PlayerApplication and SettingsScreen). I have a Service.class for Streaming which runs in the background, which holds a Notification as well (you can stop/start playback in the notification-overlay menu and the PlayerApplication). If the User clicks on the Notification, the PlayerApplicationActivity should be com back to screen.
Everything works fine, expect the case: User opens SettingsScreenActivity -> opens NotificationOverlayMenu -> clicks at Notification -> PendingIntent resumes to PlayerApplicationActivity
Now, PlayerApplicationScreen is there, but I cannot click anything. I see, that the onCreate() of my PlayerApplication isn't triggered, if I resume from Notification. But the layout looks fine (also inflated in the onCreate())
I used following PendingIntent in the Service.class:
PendingIntent contentIntent = PendingIntent.getActivity(
this.getApplicationContext(), //Service.class
0,
new Intent(this,PlayerApplicationActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP),
0);
So actually the PendingIntent should bring the Activity (if already running) back to the top. Am I using wrong Intents or am I missing a point?
EDIT:
and I declared launchMode in Manifest.xml
android:launchMode="singleTask"
According to android developer site, In FLAG_ACTIVITY_CLEAR_TOP,FLAG_ACTIVITY_SINGLE_TOP and in launch mode : SingleTask , If the instance of activity you are calling is already running then instead of creating new instance, it call onNewIntent() method. So I think you should check this condition that your activity is running when onCreate() is not calling.
I just figured out what I was doing wrong:
For my purpose I used the wrong launchMode. Right one is singleTop not singleTask
I declared the launchMode in the <application-node> instead of the <activity-node>
//Manifest.xml
<activity
android:name="..."
android:launchMode="singleTop" >