The MainActivity calls ActivityA, and then it receives a notification for ActivityA, now the back stack is [MainActivity, ActivityA], if click the notification, the current back stack should not change; If not click the notification, and back to MainActivity and then into ActivityB, the back stack is now [MainActivity, ActivityB], if click the notification now, the expected back stack is [MainActivity, ActivityB, ActivityA], but I get [MainActivity, ActivityA] instead. My code is below:
Intent backIntent = new Intent(this, MainActivity.class);
Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivities(this, 0,
new Intent[] {backIntent, intent}, PendingIntent.FLAG_UPDATE_CURRENT);
I find a similar problem in Back to main activity from notification-created activity, but it seems not suit for me.
Related
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)
My question is that I have two activities, Activity A and Activity B. A is the main activity and A is the parent activity of activity B. Activity B is accessible by touching a notification or by activity A.
Activity A start activity B like this:
Intent intent = new Intent(getActivity(), B.class);
startActivityForResult(intent, RESULT_ACTIVITY_1);
Notification starts activity B like this:
Intent openIntent = new Intent(context, B.class);
openIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntentOpen = PendingIntent.getActivity(context, 0 , openIntent, PendingIntent.FLAG_ONE_SHOT );
contentView.setOnClickPendingIntent(R.id.textView5NotifyOpen,pendingIntentOpen);
Manifest for activity B:
<activity
android:name=".B"
android:parentActivityName=".MainActivity"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.eee.ccc.MainActivity" />
</activity>
When activity B close send back to A some data:
Intent returnIntent = new Intent();
returnIntent.putExtra("Data",some data);
setResult(Activity.RESULT_OK,returnIntent);
finish();
So far everything works well, I'm able to launch activity B from A and from the notification but when I launch it from the notification when B terminate activity A is not called.
Now what I want to do is that when I click on a notifications it starts activity B and when B close/finish his parent activity A is launched with setResult(Activity.RESULT_OK,returnIntent); and onActivityResult(int requestCode, int resultCode, Intent data) on activity A is called.
THANKS!
Try TaskStackBuilder
// Intent for the activity to open when user selects the notification
Intent detailsIntent = new Intent(this, DetailsActivity.class);
// Use TaskStackBuilder to build the back stack and get the PendingIntent
PendingIntent pendingIntent =
TaskStackBuilder.create(this)
// add all of DetailsActivity's parents to the stack,
// followed by DetailsActivity itself
.addNextIntentWithParentStack(upIntent)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pendingIntent);
...
Read this android documentation for more details
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;
So what I have is a notification in my status bar which when clicked by the user brings up an activity with no title, to replicate a dialog. But I have a problem. If I open up the app, then just click the home button, and then click the notification in my status bar. It brings up apps main activity with the notificatoins activity stacked on the top. What I want to do is make it so when I click the notification it clears all the bottom activities so that never happens. Here's the code I use to start the notification activity
// Send Notification
Intent intent1 = new Intent(this, Dialog.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_quick_tweet_icon)
.setContentTitle("Dialog")
.setContentText("Touch for more options")
.setWhen(System.currentTimeMillis())
.setContentIntent(pIntent)
.setAutoCancel(false).setOngoing(true);
NotificationManager nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notif = builder.getNotification();
nm.notify(1, notif);
This isn't going to work. You've written:
Intent intent1 = new Intent(this, Dialog.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
What this says is "if activity Dialog already exists in the task stack, clear (finish) all activities that are on top of that in the stack before starting the Dialog activity. Otherwise (if there is no instance of Dialog already in the stack) just start the Dialog activity". So what you are seeing is that your Dialog activity is put on top of the other activities that are already in the task stack.
If you want this notification to remove all activities from the task stack and then start your Dialog activity, I suggest you do the following:
Create the notification like this:
Intent intent1 = new Intent(this, MyRootActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent1.putExtra("startDialog", true);
in this case, MyRootActivity must be the root activity of the task (ie: the one that has ACTION=MAIN and CATEGORY=LAUNCHER in the manifest).
In onCreate() of MyRootActivity do this:
super.onCreate(...);
if (getIntent().hasExtra("startDialog")) {
// User has selected the notification, so we show the Dialog activity
Intent intent = new Intent(this, Dialog.class);
startActivity(intent);
finish(); // finish this activity
return; // Return here so we don't execute the rest of onCreate()
}
... here is the rest of your onCreate() method...
Hopefully this is clear.
I am trying to launch an activity from the notification.
I have activity A. The activity A is launched from clicking on the notification. Once the activity A is launched, I like to get back to MainActivity upon pressing the phone's back button. To happen like that I implement as follow. The code is implemented in the BroadcastReceiver. I am having the compilation error at this line Intent resultIntent = new Intent(MainActivity.this, theClass); Because MainActivity.this is not valid in the BroadcastReceiver class. How can I make it correct?
Class theClass = Class.forName("sg.SanThit.TrackMe.NotificationListActivity");
Intent resultIntent = new Intent(MainActivity.this, theClass);
resultIntent.putExtra("MOBILENUMBER", tel);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainActivity.this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(theClass);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);