I have a function to open an Activity from the App-Widget like this:
protected PendingIntent openSettingsPedingIntent(Context context) {
Intent settingsIntent = new Intent(context, SettingsActivity.class);
settingsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return PendingIntent.getActivity(context, 1, settingsIntent, 0);
}
This works fine, but the App-Widget open the last Activity from my App, not the SettingsActivity, when I do the following steps:
Kill the app via task manager
Reopen the App via App-Widget
Switch from SettingsActivity to another Activity
Press the home button
When I now click on the App-Widget its open the last Activity I had open in my App, not the SettingsActivity.
Any ideas why this happen ?
Try replace 0 in PendingIntent with FLAG_UPDATE_CURRENT
Intent settingsIntent = new Intent(context, SettingsActivity.class);
settingsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT)
Related
I try to use local notification and it's actions. I want to create a notification and handle multiple action types. My notification asks a question to the user. There are two options, yes or no. My implementation is below:
Intent yesReceive = new Intent(this, this.getClass());
yesReceive.setAction("YES");
PendingIntent pendingIntent = PendingIntent.getActivity(this, CODE, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.icon1, "Yes", pendingIntent);
It recreates the activity. But old activity already alive. When I press back button, I can see it. How can I replace the new activity?
You can use "finish()" to close down the activity before you move onto the next one.
Here is a simple example:
startActivity(intent); <- here I am telling the program to start the desired actitvity
finish(); <- Here I am asking the program to close the current activity before I move onto the next one.
Intent yesReceive = new Intent(this, this.getClass());
yesReceive.setAction("YES");
PendingIntent pendingIntent = PendingIntent.getActivity(this, CODE, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.icon1, "Yes", pendingIntent);
finish();
I solve this problem with below line
android:launchMode="singleTop"
I create a home screen widget whenever my app launches. But when I kill the app, and click on my widget, I need to launch the app. When the app is running in background or foreground, I must be able to do actions from the widget. My code for pending intent is -
public static PendingIntent buildButtonPendingIntent(Context context) {
++MyWidgetReceiver.clickCount;
// initiate widget update request
Intent intent = new Intent();
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
return PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
//App launch if app is not running
Intent iSetting = new Intent(context, App.class);
PendingIntent piSetting = PendingIntent.getActivity(context, 0, iSetting, 0);
}
How do I do this for app killed and app launching from widget. My code is sort of incomplete.
I am currently facing the problem of setting pending action for two different activities to notification.
I have a ParentActivity and a ChildActivity. I want open ChildActivity on notification click if currently it is running or paused, otherwise start ParentActivity.
I tried this :
.........
Intent resultIntent = new Intent(this, ChildActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ParentActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
.............
Above is not working for me. Everytime ChildActivity is starting on notification click.
And also as Faruk answered, I dont want this. Creating a notification's pending intent by checking ChildActivity's current state will not work.
Suppose notification created when ChildActivity was running but after creating the notification, user killed the app. So after killing the app, If user will click on notification then ChildActivity will start. I don't want that. I want if ChildActivity is not running or paused then ParentActivity should be started.
How can I achieve this?
Please help.
While there may be several ways to achieve this, following is the one I can think of.
First, you should get whether ChildActivity is active or not, through this link
Check whether activity is active
Store this in some variable childActive, then you can initialize different notificationIntents checking the value without using task TaskStackBuilder.
For example;
Intent notificationIntent = null;
if(childActive)
notificationIntent = new Intent(context, ChildActivity.class);
else
notificationIntent = new Intent(context, ParentActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,
0, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
Have your Notification launch a simple dispatch Activity. This Activity does the following in onCreate():
super.onCreate(...);
if (ChildActivity.running) {
// ChildActivity is running, so redirect to it
Intent childIntent = new Intent(this, ChildActivity.class);
// Add necessary flags, maybe FLAG_ACTIVITY_CLEAR_TOP, it depends what the rest of your app looks like
childIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(childIntent);
} else {
// Child is not running, so redirect to parent
Intent parentIntent = new Intent(this, ParentIntent.class);
// Add necessary flags, maybe FLAG_ACTIVITY_CLEAR_TOP, it depends what the rest of your app looks like
parentIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(parentIntent);
}
finish();
In ChildActivity do this:
public static boolean running; // Set when this Activity is active
In ChildActivity.onCreate() add this:
running = true;
In ChildActivity.onDestroy() add this:
running = false;
I receive a notification in MainActivity. When I click on it, it should open the dialog fragment. Currently I am doing this -
String textNotificationMessage = textMessageReceivedEvent.getMessage();
Intent notificationIntent = new Intent(MainActivity.this, MessagingDialogFragment.class);
notificationIntent.putExtra("NotificationMessage",textNotificationMessage);
MessagingDialogFragment messagingDialogFragment = (MessagingDialogFragment) MessagingDialogFragment.instantiate(MainActivity.this, MessagingDialogFragment.class.getName());
messagingDialogFragment.show(getSupportFragmentManager(),MessagingDialogFragment.class.getName());
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
What this does is, whenever I have a notifictaion, it opens the DialogFragment automatically without a click. But I need it to open after a click. How do I achieve this?
Done like this create an activity named MyDialog.java
Now in your manifest file do like this given below
<activity
android:name=". MyDialog"
android:theme="#android:style/Theme.Dialog" />
now navigate to this activity on click event of notification.
The only way to set onClickListener on a notification is through a PendingIntent. Just make the PendingIntent open up one of your Activity and have your Activity be complete transparent and put the code of opening a dialog in onCreate() and finish() the Activity on dismiss of the dialog.
Intent notifyIntent = new Intent(context,ActivityContainingDialog.class);
notifyIntent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
//UNIQUE_ID if you expect more than one notification to appear
PendingIntent intent = PendingIntent.getActivity(SimpleNotification.this, UNIQUE_ID,
notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
I am opening an activity on clicking notification, that is working fine. But if that activity (which I am opening on clicking notification) is already open (user has opened it) then I want to close it before opening it as a result of clicking notification. So how can I do that. Please help.
Code:
Intent intent = new Intent(context, ViewReminders.class);
intent.putExtra("CALLER","GenNot");
intent.putExtra("ID",notification_id);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
Try with setting the following flags
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);