Java – getIntent.remove Extra does not work

getIntent.remove Extra does not work… here is a solution to the problem.

getIntent.remove Extra does not work

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++;

}

In HomeActivity’s oncreate, I get this extra string

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

Then on Click(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

deleted the extras, then pressed the back button to destroy the app, then reopened it by long pressing the home button in my phone, then after the extra content was still there and onClick(mPnrSearch) was called again, but I had to delete the excess Why is this so?? What do I need to do to fix this.

Solution

This is an Android bug or feature, depending on whether you want it to happen 😉 This situation is not clearly documented, but it is clear that it behaves in the same way as you described.

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

Related Problems and Solutions