Current Activity finished when I build a notification - android

I want to start a notification in current Activity (ChatActivity),when I touch the notification ,I want to enter the ChatOneActivity.
However I don't want the current Activity (ChatActivity) finished when I'm in the ChatOneActivity(Because I'm receiving the data).And when I press the back button,I want to stay in the ChatActivity.
(The point is I do not want the ChatActivity finish ,no matter which activity I am current in).
So what should I do?
Here is the code
private void showNotification(String id, String message) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(android.R.drawable.sym_action_email)
.setContentTitle("You have a new message")
.setContentText(message);
Intent intent = new Intent(ChatActivity.this,ChatOneActivity.class);
intent.putExtra("toId", id);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(ChatActivity.this);
stackBuilder.addParentStack(ChatOneActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, builder.build());
}
Now I enter the ChatOneActivity,when I press the back button,I return to the desktop. means that the ChatActivity has already finished,which I don't want

I believe you can't do what you want.
First of all, take a look to this link https://developer.android.com/guide/components/tasks-and-back-stack.html worth a read.
You are opening ChatOneActivity from ChatActivity and it will be saved in the Back Stack, Android system can kill your activity when memory is needed but it will be recreated.
Also, you are adding ChatOneActivity as a parent in the back stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(ChatActivity.this);
stackBuilder.addParentStack(ChatOneActivity.class);
If you are in the parent activity and the back button is pressed, is reasonable that the app will close.
If you want to open Activity2 from Activity1 and when you press back return to the Activity1 you shouldn't have to do anything, the Android Back Stack will manage the behavior.

Related

Tap on Notification from locked screen opens Wrong Activity first

Below function displays a notification which pending intents. When the screen is unlock and receive notification, below method will create a notification.
When user tap on notification, it opens first ChildActivity, On backpress, it closes child activity and opens MainActivity->subActivity. on backpress closed subActivity and shows MainActivity.
Subactivity will be started based on the Intent action of MainActivity
In the unlock screen, below method gives above result and its perfect as per my requirement.
public void showNotification(Context context, String extraUri){
final Intent mainActivityIntent = new Intent(context, MainActivity.class);
mainActivityIntent.putExtra(MainActivity.INTENT_EXTRA_ACTIVE, 1232123);
// this will help determine to start subactivity from mainActivity
mainActivityIntent.setAction(MainActivity.MAIN_ACTIVITY_CHANGED);
mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
taskStackBuilder.addNextIntent(mainActivityIntent);
final Intent intent = new Intent(context, ChildActivity.class);
intent.putExtra(ChildActivity.EXTRA_URI, extraUri);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
taskStackBuilder.addNextIntent(intent);
final PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText("Message Body")
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
Problem
while screen is locked and tap on the notification, it ask to unlock the screen, on unlock, it starts the app. but
very first it shows SubActivity, than on back press it shows ChildActivity and on backpress of ChildActivity it shows MainActivity.
So the order of the Activities are changed.
the correct order is
MainActivity->SubActivity->ChildActivity (TopMost) (happens in unlocked screen)
Thanks in Advance.

strange behavior when starting activity from notification

I am using Parse to send notification to Android phones and when I receive push I will generate a notification using the below function.
private void generateNotification(Context context, String title, Intent intent, boolean hasSound) {
int NOTIFICATION_ID = ("onccent" + System.currentTimeMillis()).hashCode();
PendingIntent contentIntent = PendingIntent.getActivity(context, NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager mNotifM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher).setContentTitle(context.getString(R.string.app_name)).setContentText(title);
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(contentIntent);
if (!hasSound) {
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
}
mNotifM.notify(NOTIFICATION_ID, mBuilder.build());
}
The intent is a intent of a activity displaying messages and let's call it Activity B. I will use Activity A to represent the home activity.
The problem is: when A exists, all things work well. When I click a notification, the stack becomes
A -> B1
And click another:
A -> B1 -> B2
This is what I expect. However, when I kill the whole app and then click on the first notification. It will open a B but the following notification will only bring my app to front without open a new B.
It seems something will not work without A. I manage to open A first if A does not exist in the stack so that define A as the parent activity of B. The notification works but it seems all previous activity goes away.
How can I solve this? In short, I just want that a new activity will open when I click on the notification.

Android - Send notification with id

Please excuse my English, I'm French :
I've got a question about android notifications...
There is an inbox on my app and user is notified when he received a new notified.
When he click on the notification, I want to open the right activity with the right conversation so I've to pass an ID.
I've made a lot of search but I can't find...
Coul'd you help me please ?
Here my code :
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setAutoCancel(true)
.setContentText(text);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, LandingActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_CANCEL_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());
Thanks.
In this case, you could save the ID of the conversation in the Intent that will launch your app. You do this by using extras:
resultIntent.putExtra("conversationID", "234553");
Then, in your MainActivity, you handle the case where the user has clicked on your notification:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("conversationID");
//Display conversation
}
I don't know if I get your question right: If you want to start different Activities according to something you received, you have to make different Intents for those cases.
If you always want to open your LandingActivity and pass data to it, add them as extras to your Intent, see How do I pass data between Activities in Android application?.

Preserve back activity while clicking notification

My problem is like that: When I'm pressing the notification i got, some activity is open, lets say activity number 3. But when I press the back button, I navigated by the android to the menu. What I want to do is to preserve the correct navigation experience of the user, and when I'm pressing the back button it will take me to activity number 2, and when I'm pressing there on the back button, it will take me to activity number 1, and so on.
Another problem I have is that, when I'm opening activity number 2, I need to set there some extra data in the intent, but I don't know how to set it.
Summery of my problem: preserve the correct navigation of the user and set some extras for the activity that will be open by the back button click.
Here is my notification code:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("New Question")
.setContentText("" + CurrentQuestion.getAuthor().getUserName() + ": " + CurrentQuestion.getQuestion() + "");
Intent openHomePageActivity = new Intent("com.example.ihelp.HOMEPAGEACTIVITY");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(openHomePageActivity);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
Thanks!
Looks like you want to synthesize a back stack: http://developer.android.com/training/implementing-navigation/temporal.html#SynthesizeBackStack (see "Create back stack when starting the activity")
and also http://developer.android.com/guide/topics/ui/notifiers/notifications.html#NotificationResponse ("Preserving Navigation when Starting an Activity" as a "Regular activity")
Anyone in same scenario can use this solution that I use in my apps.
Override onBackPressed (in your activity routed from notification) and check if your app is in stack or not. if not then start your root activity. User can always navigate from your root activity. This is what I do in my apps
#Override
public void onBackPressed() {
if (isTaskRoot()) {
Intent intent = new Intent(this,YourMainActivity.class);
startActivity(intent);
super.onBackPressed();
}else {
super.onBackPressed();
}
}
Save the parameters you need in onSaveInstanceState & get them back in onRestoreInstanceState. that way it won't matter if your activity was paused by any other notification as well.

How to launch activity from click on notification?

How do I launch an activity when the user clicks on the notification? I am having constant crashes. I can not get the click on the notification to work. There is an onclick method shown below. When a button called ButtonOne is pressed, it will launch a notification in the top menu bar of the screen. I wanted to user to be able to press the notification and have it launch the activity called MainActivity. It crashes and will not launch the page. There is probably something wrong in my code for notification that I put inside of the MainActivity class. What is wrong?
ButtonOne.setOnClickListener(new View.OnClickListener() {
private int mId;
// anonymous inner class override for on click
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, CoverFlowExample.class);
MainActivity.this.startActivity(myIntent);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(MainActivity.this, MainActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainActivity.this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// 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);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
}
});
Found out that the problem was the AVD android emulator not the code. For some reason it did not update the code from earlier version. Tested it again and now it runs with no errors.

Categories

Resources