IssacZH.
IssacZH.

Reputation: 1477

Notification intent activity

I got some issue regarding about the notification intent. I have 2 activity (A & B). Activity A is the main activity of my application. The first activity that user will go through. As for activity B is when the user enter by clicking on a button at Activity A.

The thing is when I clicked on my notification and it will direct me to Activity B. Then direct me back to Activity A onBackPressed. But when I close the application and open it back through the multi-tasking option, it resume my application at Activity B. I wanted the application to start back at Activity A instead after closing it.

The order should be

Activity A --> Activity B.

Notification onClick -> Activity B (onBackPressed) -> Activity A --> Close.

Re-open the app / open with multi-tasking feature --> Activity A

Please do let me know if there is any other information that I can provide for a proper understanding to my question.

GCM_Intent.class

    Notification note = new Notification(icon, msgTopic ,System.currentTimeMillis());
    Intent i=new Intent(this, Activity_B.class);
    i.putExtra("topicId", topicId);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
    Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pi=PendingIntent.getActivity(this, CommunitiesappConstant.NOTIFICATION_ID, i,  PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
    note.setLatestEventInfo(this, topicName ,msgInfo+message, pi);
    note.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
    note.ledARGB |= 0xff0000ff;
    note.ledOffMS |= 1000;
    note.ledOnMS |= 300;


    mgr.notify(CommunitiesappConstant.NOTIFICATION_ID, note);

Activity B.class

@Override
public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_b);
    Bundle bundle = getIntent().getExtras();
    topicId = bundle.getString("topicId");

}

@Override
public void onBackPressed() {
    super.onBackPressed();
    Log.e(TAG,  "onBackPressed");
    Intent i = new Intent(Activity_B.this, Activity_A.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
    finish();
}

Upvotes: 0

Views: 1711

Answers (1)

Robert  Castaneda
Robert Castaneda

Reputation: 39

Notification is a message you can display to the user outside of your application's normal UI.This is perfect code to use of notification.

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.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(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.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());

Upvotes: 1

Related Questions