简体   繁体   中英

Android clicking launcher icon starts a new Activity ONLY if issued after starting that activity from notification

Launching an activity by clicking the app icon in launcher, should bring the activity to foreground just like picking it from history. So no onCreate call should exist.

However,if we try to do this after starting the activity by clicking a notification, then the launcher just starts another instance of the activity.

What flags must I add so that the launcher keeps working as expected ( resuming the exact state of the app from background )?

I'll post the essential code.

This starts the notification:

Intent resumeIntent = new Intent(this, MainActivity.class);
PendingIntent resumePendingIntent = PendingIntent.getActivity(this, 2,
        resumeIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Notification resumeNotification = new Notification.Builder(this).setContentTitle(
        "Resume style")
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentIntent(resumePendingIntent)
        .build();

NotificationManager notificationManager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
notificationManager.notify(1, launcherNotification);

This is how the manifest activity looks:

    <activity
        android:name="com.example.ihatenotifiicationsapp.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

This resumeIntent will be automatically added the FLAG_ACTIVITY_NEW_TASK by Android. This flag will allow the resuming of the application from background if present.

All nice until here, but, If after you click this notification and resume the app, you click the app from launcher then Android launches another instance of MainActivity.

This breakes my application and the backstack ( you will have 2 MainActivity in the stack, weird for user ).

The funnies thing is this happens ( clicking the launcher behaviour to launch another instance ) only after you click the notification.

You can use the Flag android:launchMode="singleTask" in your activity Tag if you want this behavior. This prevents the OS from launching any other Instance, if there is currently one Active. See the SDK Doku for more information on launchbehaviors here

I edited this Answer corresponding to Emanuel Moecklin Comment below. Mixed the lauchModes up.

Excerpt from the Doku:

The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.

Try

Intent resumeIntent = new Intent(this, MainActivity.class)
                         .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP;

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent. http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM