4
public void showNotification(Context context,String pnrNumber){

        Intent intent=new Intent(context,HomeActivity.class);
        intent.putExtra("PNR", pnrNumber);

        //To Clear the Activity Stack
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

        PendingIntent contentIntent = PendingIntent.getActivity(context, uniqueNumber,intent, Intent.FLAG_ACTIVITY_CLEAR_TASK);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("TravelKhana")
                .setContentText("Get food in train for the PNR:" +pnrNumber);
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(uniqueNumber, mBuilder.build());
        uniqueNumber++;

    }

and in oncreate of the HomeActivity i am getting this string extra

if(getIntent().hasExtra("PNR")){
                mPnrSearch.setTag(getIntent().getStringExtra("PNR"));
                onClick(mPnrSearch);
            }

And then in onClick(mPnrSearch);

public void onClick(View v) {
        switch (v.getId()) {
        case R.id.pnrSearch:
            if(NetworkChecker.isConnected(getApplicationContext())) {
                easyTracker.send(MapBuilder.createEvent("Home Activity","click", "PNR", null).build());
            }
            Intent pnrIntent = new Intent(HomeActivity.this, PnrSearch.class);

            //If the user came from notification
            if(v.getTag() != null){
                pnrIntent.putExtra("PNR", v.getTag().toString());
                v.setTag(null);
                getIntent().removeExtra("PNR");
            }

            startActivity(pnrIntent);
            break;
}

I removed the extra and then I pressed the back button to destroy the app and reopened it by long pressing the home button in my phone and then after the extra is still there and onClick(mPnrSearch) is called again, but i have removed the extra why is it so?? and what do i need to do to resolve this out.

Paras Dhawan
  • 364
  • 8
  • 23

3 Answers3

4

This is either an Android bug or a feature, depending on whether you want it to happen or not ;-) This case isn't clearly documented, but it is clear that it behaves the way you describe.

I recently answered a similar question and made some suggestions about how to deal with this problem. See my answer for more information.

Community
  • 1
  • 1
David Wasser
  • 93,459
  • 16
  • 209
  • 274
0

i think after removing you should update new intent, just implement this function in your activity

 protected void onNewIntent(Intent intent) {
        // TODO Auto-generated method stub
        super.onNewIntent(intent);
        setIntent(intent);
    }
MichaelP
  • 2,761
  • 5
  • 31
  • 36
0

in my case i passed the value from notification to my MainActivity from onMessageReceived. why because i wanted to count the opening app by notification click for specific notification id which is the value i passed by exteras( notification opens the app mainactivity ) for analytics reasons. what ever, to find out if main activity is opened by notification or not i checked both exteras value and intent flag. i observed that when ever app is opened by notification click the intent flag is 0 so i added the below to my mainactivity.

if(getIntent().getFlags() == 0 && getIntent().getExtras() != null){
            Log.i("opened by notification only",getIntent().getStringExtra("myID"));
            //rest of process...............
        }
Rouzbeh Zarandi
  • 1,047
  • 2
  • 16
  • 34