I'm having problem in my application. I have a notification that is being push in action bar. When you click the notification, you will be redirected to the specific activity that you set. In my scenario, once the notification is being clicked or touch, I should be redirected to the specific fragment of my current activity.(current activity is handling the fragments)
Now, this is were i stock,I made tried something like this but no luck.
-when notification is clicked, it will launch another activity(NotificationControllerActivity) which when loaded, it will call the currect activity and display the fragment(a public method to load fragments).My error is, I cant cast NotificationControllerActivity into currentactivity.(i'm lookinf for solutions).
I would really appreciate your feedback.
thanks
//Set pending intent for notification
Intent intent = new Intent(this, RequiredActivity.class);
intent.putExtra("key1", value);
intent.putExtra("key2", value);
PendingIntent pIntent = PendingIntent.getActivity(this,0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
// And in Required Activity override method onNewIntent(),and open up your required fragment.
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 have some troubles with the management of recommendations (notifications) on an Android TV with the Leanback Launcher.
I use the google sample here to implement my own : https://github.com/googlesamples/androidtv-Leanback
Expected result :
I'm on my activity "MainActivity" (with a webview)
I press HOME, so I'm on the Leanback launcher with recommendation include mines.
I press on one of them
Resume activity "MainActivity" without recreate it with a new Intent with a new extra.
Actually, The resume of the activity without reload the activity works fine, below the creation of the PendingIntent :
private PendingIntent buildPendingIntent(Parcelable media, int id) {
Intent detailsIntent = new Intent(this, MainActivity.class);
detailsIntent.putExtra("media", media);
detailsIntent.putExtra("id", id);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(detailsIntent);
return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
}
Current result :
I'm on my activity "MainActivity" (with a webview)
I press HOME, so I'm on the Leanback launcher with recommendation include mines.
I press on one of them
My activity receive "onNewIntent" event by in the extrat intent values I have always the same media.
This solution doesn't work, because Google said in a comment of the sample code :
// Ensure a unique PendingIntents, otherwise all recommendations end up with the same
// PendingIntent
detailsIntent.setAction(movie.getId());
So to differentiate all recommandations, I have to set Action with an ID, else, it will be always the last pendingintent sent to my activity. It's my current behavior, I receive in "onNewIntent" always the same "media" and "id" in the intent (the last one), whatever on which recommendation I click, I always get the same Intent.
But if I set the action with an "id", the activity "MainActivity" is recreated, so the resume failed, and the context of my webview is cleared my webview is reloaded :( but I get the good intent with the good media in the extra intent values.
Have you a solution to help me to have the behavior I want ?
To simplify my question, how can I resume my Activity B from a recommendation with the good pendingintent without reload my activity ?
Thank you in advance for your help.
I found a solution. I don't think it's the good one but it works for my case.
PendingIntent pIntent = PendingIntent.getActivity(this, id, detailsIntent, PendingIntent.FLAG_UPDATE_CURRENT);
return pIntent;
So, If I set the "resquetCode" with the id, so Android think it's a different Activity and resume it without recreate a new one.
I wrote an app, that brings up a notification every certain time via an alarmIntent. The user clicks on the notification and gets into an activity. After doing something in this activity he should press a button to return to the original app he was in (like a browser, or the homescreen, etc.), before clicking on the notification.
My problem is, that the user always returns to the main activity of my application.
How can I change that?
Thank you!
try adding this lines in your Activity
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
System.exit(0);
}
So I found the solution finaly:
you need to add the Intent.FLAG_ACTIVITY_MULTIPLE_TASKS flag to the intent as explained in that question:
Go back to previous screen on backbutton pressed after responding to notification
Try Activity.finish() inside the button click listener function. This is similar to pressing the back button.
There are two types of Activity started by Notification : regular & special.
Regular Activity always return to Launcher or your previous activity.
Special Activity will return to your Application.
As your description, you should use Regular Activity and get PendingIntent by TaskStackBuilder, it will create an artificial back stack for notification activity.
for example :
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
And this is the document, you can find detail here.
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
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 have a normal notification system that looks like this:
Notification notification new Notification(
R.drawable.alerts_notification,
alertTitle,
System.currentTimeMillis());
Intent intent = new Intent(mContext, MyActivity.class);
intent.setAction(MyActivity.ONE_ACTION);
PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);
mNotifMan.notify(ID, notification);
Notice that I'm using ONE_ACTION as the action of the intent. What I do is verify the action on the activity and select one of the tabs (it's a TabActivity).
All that works fine if the activity is closed, because the Intent will open the activity and then I will decide what to do depending on the action in the Intent. But, if the activity is already opened, it launches a new activity. On the other hand, if I add the flag Intent.FLAG_ACTIVITY_SINGLE_TOP, the activity is not launched twice but I can't the tab is not chosen either.
So, how can choose a tab by clicking on the notification?
Have your intent open your tab activity and put an extra in it denoting the tab. When you detect action resume get a handle on your tab controller and change the tab through code.
OK, I found how to do it... it seems I hadn misread the documentation. What I did was:
Add this to the activity in the AndroidManifest.xml file: android:launchMode="singleInstance"
Overwrite the onNewIntent(Intent intent) method on the activity and put there all the logic to select the tabs etc.
Launch the intent with the flag Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
#schwiz, thanks for your answer though.