I have a problem. When I call finish() , the activity of the method is saved in the task manager, and if the user reloads it from the task manager, my activity gets the old intention. If this intention was sent from a push notification, I had an undesirable reaction: my intention was to start the process with the push notification data.
How to properly manage the behavior of push notifications in my activity in order to avoid an incorrect activity state?
My application receives a push notification and a form in anticipation of the intention of the reaction when pressed:
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 the user came to the application from push, the application will obviously get the intention using 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 the 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); } }
Manifest activity declaration:
<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>
android android-intent
Stanislav
source share