Search code examples
androidandroid-fragmentsandroid-intentandroid-notificationsback-stack

Android - Resume backgrounded app when my app finishes


Basically, I'm browsing the internet in chrome or looking at another app. I get a notification that opens my application. I do some stuff and it finishes. My application closes and i'm back on the launcher. How can I get my app to finish and return me to whatever previous app/chrome page i was browsing?

AndroidManifest section for this activity:

<activity
        android:name=".AcceptRejectActivity"
        android:label="@string/title_activity_accept_reject"
        android:screenOrientation="portrait" >
    </activity>

Notification code:

mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent resultIntent = new Intent(this, AcceptRejectActivity.class);
    resultIntent.putExtra("message","Do you choose to accept this message?");      

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(AcceptRejectActivity.class);

    // Adds the Intent that starts the Activity to the top of the stack //
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent contentIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    String msg = "You have a message"
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_logo)
                    .setContentTitle("Message")
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(msg))
                    .setContentText(msg)
                    .setPriority(Notification.PRIORITY_HIGH);
    mBuilder.setContentIntent(contentIntent);
    mBuilder.setAutoCancel(true);

    android.app.Notification note = mBuilder.build();
    note.defaults |= android.app.Notification.DEFAULT_VIBRATE;
    note.defaults |= android.app.Notification.DEFAULT_SOUND;        

    mNotificationManager.notify(0, note);

Fragment's close function

private void allDone() {
        Log.i(TAG, "The choice has been made");            
        getActivity().finish();
        //NavUtils.navigateUpFromSameTask(getActivity());
    }

Solution

  • I managed to solve my issue.

    Android Manifest:

    <activity
            android:name=".AcceptRejectActivity"
            android:label="@string/title_activity_accept_reject"
            android:screenOrientation="portrait"
            android:launchMode="singleInstance"
            android:excludeFromRecents="true">
        </activity>
    

    Notification code: Changed how the pendingIntent was created.

    mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent resultIntent = new Intent(this, AcceptRejectActivity.class);
    resultIntent.putExtra("message","Do you choose to accept this message?");      
    
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    String msg = "You have a message"
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_logo)
                    .setContentTitle("Message")
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(msg))
                    .setContentText(msg)
                    .setPriority(Notification.PRIORITY_HIGH);
    mBuilder.setContentIntent(contentIntent);
    mBuilder.setAutoCancel(true);
    
    android.app.Notification note = mBuilder.build();
    note.defaults |= android.app.Notification.DEFAULT_VIBRATE;
    note.defaults |= android.app.Notification.DEFAULT_SOUND;        
    
    mNotificationManager.notify(0, note);
    

    Now when I'm done my processing I can just call finish() or getActivity().finish() and it will take me back to whatever app/browser page you were on before clicking the notification.