0

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());
    }
ChoklatStu
  • 229
  • 1
  • 10

2 Answers2

0

Well that´s a little bit weird, but like an option you can open the browser after the finish your application.

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

      try { //Open browser!.
            Uri uri = Uri.parse("googlechrome://navigate?url=");
            Intent i = new Intent(Intent.ACTION_VIEW, uri);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
        } catch (ActivityNotFoundException e) {
            // Chrome is not installed
        }

}

Then you will return to your Chrome session ;).

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • Yeah, it's a little weird. Calling finish() use to close my app and return me to whatever app I was in. Kind of like a cross app onbackpressed. Then one day it stopped working. This relaunches chrome, but doesn't bring me back to my current page. Still doesn't solve if i'm in facebook, inbox, or any other app. – ChoklatStu Feb 10 '16 at 19:29
  • this will show you the last opened page in chrome, if you want to open an specific page just add the url, for example: ?url=http://www.facebook.com – Jorgesys Feb 10 '16 at 19:33
0

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.

ChoklatStu
  • 229
  • 1
  • 10