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
Related
code create notification from BroadcastReceiver
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(G.context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(G.context.getString(R.string.app_name))
.setContentText("text")
.setAutoCancel(true)
.setColor(Color.MAGENTA);
Intent intent = new Intent(G.context, B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(G.context);
taskStackBuilder.addParentStack(MainActivity.class);
taskStackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) G.context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(2, mBuilder.build());
If you are currently in MainActivity.java and then click on notification and go to B Activity the just simply call this.finish() or onBackPressed() in your back press control onClick() method. If your are opening your B Activity from any where by clicking one notification then Call below method in your B activity.
#Override
public void onBackPressed() {
Intent i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
Other way you can do like this
You Dont need do anything. By default when you move from Activity A to Activity B, Android adds the Activity A to the backstack. When you press the back button from Activity B or finish it, it automatically restore the Activity A from backstack.
If you wish to finish your Activity B programmatically call activity's finish() method when you want to do it.
For more detail Check this
I am trying to start my activity from the notification and it is working but instead of starting the same activity, another (copy) of my activity gets launched when i exit from it I find another copy of the activity beneath it.
NotificationCompat.Builder b = new NotificationCompat.Builder(this);
....
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("item", currentObj);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
....
b.setContentIntent(pIntent);
how can I fix this ??
you have to use TaskStackBuilder and add its parent
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
and you have add it in the notification
stackBuilder.addParentStack(MainActivity.class);
What it does is simple it just says android about the parent of the present class you have opened! This should slove your problem. I hope this is helpful. Thankyou
You can call finish(); before b.setContentIntent(pIntent);.
There is a flag you can set that starts the same activity instead of a new one each time:
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("item", currentObj);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
You can add this line to your Activity inside manifest to prevent from loading the same activity multiple times
android:launchMode = "singleInstance"
OR
Flag the intent before starting Activity like this
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
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.
I'm having trouble resuming an activity.
Here's what happening:
Activity A calls activity B :
Intent intent = new Intent(A.this, B.class);
startActivity(intent);
Then Activity B Sets a Notification and calls Activity A:
Intent intent = new Intent(this, MrpCastPlayerActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, 0);
android.support.v4.app.NotificationCompat.Builder notif = new NotificationCompat.Builder(this);
notif.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("TITLE")
.setContentText("CONTENT")
.setAutoCancel(true);
notif.setOngoing(true);
Notification notification = notif.build();
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationmanager.notify(0, notification);
Intent intent2 = new Intent(B.this, A.class);
startActivity(intent2);
The problem is when I tap the notification it opens Activity B but it does't call the onResume it calls onCreate.
If I use moveTaskToBack(true), instead of starting intent2, onResume is called.
It seems that Activity B is finished when I use intent2 to open Activity A.
Is there a away to avoid this ?
To resume your activity, you should considering adding flags to your Intent.
do nothing inside the method onResume() and onstart() when coming back to this activity, Try this
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Manifest Changes:
<activity android:name=".MyActivity"
android:configChanges="keyboardHidden|orientation">
Hope this helps
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);