Intent extras being lost when sending with Pending

2019-08-06 04:35发布

问题:

I am creating a notification but when sending it using PendingIntent the extras get lost in the process. I am using a singleTop Activity, so I set a break point at the onNewIntent and also my handleIntent to see which one was firing up when I clicked on the notification.

When I clicked the notification, it fired up my handleIntent method. I thought a singleTop activity will fire the onNewIntent method?

Why are my Intent extras being lost in the process when creating the notification? I debugged the intent and it shows Bundle[mParcelledData.dataSize=168]. How do I unparcel the Bundle?

Activity that handles the intent

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setVolumeControlStream(AudioManager.STREAM_MUSIC);

    mPrefs = new SharedPrefs(this);
    mDb = DatabaseManager.getInstance(this);
    mVolleyManager = new VolleyManager(this);
    fm = getSupportFragmentManager();

    setupToolbar();
    setupNavDrawer();
    setupBroadcastReceivers();
    setupUniversalImageLoader();

    if (savedInstanceState == null) {
        FragmentTransaction ft = fm.beginTransaction();
        ft = ViewManager.setFragmentAnimations(ft);

        if (!mPrefs.getDemoMode()) {
            if (!mPrefs.getLoggedIn()) {
                ft.add(R.id.container, LoginFragment.newInstance(), GlobalVars
                        .TAG_LOGIN_FRAGMENT).commit();
            }
            else {
                ft.add(R.id.container, BrowseFragment.newInstance(), GlobalVars.
                        TAG_BROWSE_FRAGMENT).commit();
            }
        }
    }

    // Handles intents mainly sent from GCM notification click events
    handleIntent();
}

@Override
public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    handleIntent();
}

private void handleIntent() {
    Intent intent = getIntent();

    if (intent.hasExtra(GlobalVars.KEY_EVENT)) {
        handleGcmIntent(intent);
    }
}

Adding the extras to the intent and creating the notification

        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra(GlobalVars.KEY_EVENT, events);
        intent.putExtra(GlobalVars.KEY_GAME_DATE_ID, gameDate.getId());

        String text = getResources().getString(R.string.msg_found_match);
        createNotification(text, intent);

private void createNotification(String text, Intent intent) {
    mNotificationManager = (NotificationManager) this.getSystemService(Context
            .NOTIFICATION_SERVICE);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("LovelUp")
            .setContentText(text)
            .setAutoCancel(true)
            .setLights(R.color.primary_blue, 2000, 2000);

    if (mPrefs.getNotificationVibrate()) {
        mBuilder.setVibrate(new long[] {1000, 1000, 1000});
    }
    if (mPrefs.getNotificationSound()) {
        mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    }

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(intent);

    PendingIntent contentIntent = stackBuilder.getPendingIntent(0, PendingIntent
            .FLAG_CANCEL_CURRENT);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

回答1:

You may want to use putStringArrayListExtra to add ArrayList to the bundle.

In your case:

intent.putStringArrayListExtra(GlobalVars.KEY_EVENT, events);

and then use getStringArrayListExtra to retrieve data.

More info here (search for putStringArrayListExtra)



回答2:

This is because Intent.putExtra(String key, <Whatever> value) doesn't accept ArrayList as a value. Your code goes past compiler because ArrayList is Serializable, it is accepted but with ArrayList something else is wrong.

You should use [putStringArrayListExtra()][1]