Search code examples
javaandroidandroid-intentandroid-pendingintent

Launch a new activity from notification bar when that same activity is currently being used


I launch a specific Activity from a button in notification. If I'm not currently opening that specific Activity, it will launch the Activity as a new one. However, when I'm opening that specific Activity, scroll down the notification then click that button, it would do nothing as the Activity already opened. I need to open it as a new one as new values will be passed into it. I could use SharedPreferences to do that but I prefer not to. Please help

        intent = new Intent(context,AppReport.class);
        intent.putExtra("name",packageInstallName); //this does not get send over
        intent.putExtra("version",packageInstallVersion);
        PendingIntent testing = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
        setOnClickPendingIntent(buttons[1],testing);

This is how the activity is launched from notification bar. I tried FLAG_UPDATE_CURRENT for the flag but also didn't work.


Solution

  • Make your Activity as singleTop

    <activity
            android:name=".AppReport"
            android:configChanges="screenSize|keyboard|orientation|screenSize"
            android:label="@string/app_name"
            android:launchMode="singleTop"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="stateHidden"/>
    
    intent = new Intent(context,AppReport.class);
        intent.putExtra("name",packageInstallName); //this does not get send over
        intent.putExtra("version",packageInstallVersion);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent testing = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    Override onNewIntent(Intent intent) in Activity.

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        //Do your stuff
    }