Android : Intent manage. Old intent is resend if user open application from task-manager

on Monday, August 18, 2014


I have a problem. When i call finish() method activity still hold in task-manager, and if user restart it from task-manager my activity receive old intent. If that intent was sent from push-notification I have unwanted reaction: my app start process intent with push-notification data.


How correctly manage push-notification intent behavior in my activity for avoid wrong activity state?


My app receive a push-notification and form pending intent for reaction on push:



final NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

int defaultOptions = Notification.DEFAULT_LIGHTS;
defaultOptions |= Notification.DEFAULT_SOUND;

Intent intentAction = new Intent(context, MainActivity.class);
intentAction.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intentAction.putExtra(BUNDLE_COLLAPSE_KEY, data.getString(BUNDLE_COLLAPSE_KEY));
intentAction.setAction(CUSTOM_ACTION);

final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intentAction, PendingIntent.FLAG_UPDATE_CURRENT);
int notificationFlags = Notification.FLAG_AUTO_CANCEL;
final Notification notification = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.icon_splash)
.setContentTitle(context.getResources().getString(R.string.app_name))
.setContentText(data.getString(BUNDLE_PUSH_CONTENT_DATA))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setDefaults(defaultOptions)
.getNotification();
notification.flags |= notificationFlags;
mNotificationManager.notify(NOTIFICATION_ID, notification);


after user came in app from push, application, obviously, receive intent with CUSTOM_ACTION and do some work:



private void intentProcess(Intent intent) {
boolean customAction = intent.getAction().equals(GCMPushReceiver.CUSTOM_ACTION);
if (customAction) {
//push reaction, do some staff with intent
} else {
//no push reaction, user just open activity
}
}


I call intentProcess method from onCreate and from onNewIntent:



public class MainActivity extends FragmentActivity {
//this case if my app closed and user tap on push
@Override
protected void onCreate(Bundle savedInstanceState) {
/* ... */
intentProcess(getIntent());
}
//this case if my app opened and user tap on push
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
intentProcess(intent);
}
}


Activity declaration in manifest:



<activity
android:name=".ui.activity.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

0 comments:

Post a Comment