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!
Related
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.
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.
following scenario:
I have 3 of the same widgets on my home screen. If one gets clicked, the widget configuration activity gets launched.
This was implemented by following code:
Intent intent = new Intent(context, WidgetConfigurator.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent pendIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteView.setOnClickPendingIntent(R.id.widget_linearlayout, pendIntent);
The launching is working, but there is one problem:
1. Widget A gets clicked, configuration activity of Widget A is opened
2. User hits "back" key, configuration activity disappears
3. Widget B gets clicked, configuration activity of Widget B is opened
4. User hits "back" key
=> Now the configuration activity of Widget A is shown
I always only want the "actual" configuration activity (fitting to the widget that was clicked) to be shown. Which settings do i have to use for the Intent / PendingIntent?
thx for any help
Seems after trying around a lot i now found a working solution for all android versions:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent pendIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteView.setOnClickPendingIntent(R.id.widget_linearlayout, pendIntent);
Change your pending intent code to include the widget id as android reuses intents. I wasted many hours on this with the wrong intent being sent on click.
PendingIntent pendIntent = PendingIntent.getActivity(context, WIDGETID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
A way to get sure your current activity gets closed is to call finish(). An android app is build like stack, the activity A starts, if the user hits back, this activity isn´t closing. It´s just lying under the new started activity B. If user hits back in activity B, then activity A will be shown. I never tried this, but if You call finish in your onBackPressed method, the current activity will be closed. To do this, override onBackPressed in your Activitys.
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.
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.