0

in my android application i'm using a service to check every 10 seconds if there is a new message in the mysql database, and give me a notification, so when i click on the notification the messages activity start, but when i click on the back button i go out of the application instead of going back to HomeScreen

//show notification
public void ShowNotification(){
    NotificationCompat.Builder nbuild= new NotificationCompat.Builder(this)
            .setContentTitle("Message :"+username)
            .setContentText(messagetext)
            .setSubText(messageadddate)
            .setLargeIcon(bitmap)
            .setAutoCancel(true)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setSmallIcon(R.drawable.ic_stat_notification_red);
    Intent showMessages = new Intent(this, ShowMessages.class);
    TaskStackBuilder builder = TaskStackBuilder.create(this);
    builder.addParentStack(HomeScreen.class);
    builder.addNextIntent(showMessages);
    PendingIntent contentIntent = builder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    nbuild.setContentIntent(contentIntent);
    MessagesNotifManager.notify((int) System.currentTimeMillis(), nbuild.build());
}
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Are there any messages in the logcat to indicate that something went wrong? – David Wasser Sep 01 '16 at 17:47
  • Possible duplicate of [Android - Build a notification, TaskStackBuilder.addParentStack not working](http://stackoverflow.com/questions/13632480/android-build-a-notification-taskstackbuilder-addparentstack-not-working) – David Wasser Sep 01 '16 at 17:50
  • You aren't adding the parent `Activity` correctly. See the linked question and make sure that you are have the parent `Activity` correctly defined in the manifest. – David Wasser Sep 01 '16 at 17:52

2 Answers2

0

It is normal, the back button by default navigate the back stack and your home activity is not in the backstack.

Here is a guide: https://developer.android.com/guide/components/tasks-and-back-stack.html

if you want to force the home activity to open, so in the activity put:

@Override
public void onBackPressed() {

    // start the intent(your home screen) you want, and finish() this. 
  super.onBackPressed(); // remove this.  
}
letrounet
  • 91
  • 1
  • 3
  • If you look at OP's code, he is using `TaskStackBuilder` to build a back stack so that his `HomeScreen` will show when `ShowMessages` finishes. – David Wasser Sep 01 '16 at 17:46
0

I presume HomeScreen what you are talking about in this context is your Application home screen (lets call as MainActivity). Well.. Logically only you know that MainActivity is controlling all other activities.. Android doesn't know the flow of your activities.

Since you called only notification Activity from notification bar...

 Intent showMessages = new Intent(this, ShowMessages.class);

android wouldnt know that it has to go back to MainActivity

However, i would use this workaround... May not be the best... but it will server the purpose...

@Override
public void onBackPressed() {
    Intent intentx;
    intentx = new Intent(thisContext, DashboardMainActivity.class);
    intentx.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    thisContext.startActivity(intentx);
    finish();
}
Android Rao
  • 157
  • 1
  • 10