bugfixr
bugfixr

Reputation: 8077

Pressing back/home from activity launched in notification exits app

I've got an activity that I launch from a notification. I include a back-stack using TaskStackBuilder so that when the user hits the home button (actionbar title button) or uses the back key, it will return to the application. However, it isn't working in that manner, instead hitting back or the actionbar title button always results in the app closing.

For what it's worth, my project structure is organized such that all UI components are in an Android Library (com.be.commotion) and an outside "wrapper" project uses that Library.

Here's how I build the notification:

// This is the activity that will be launched when tapping the notification
Intent intentNotification = new Intent(this, NowPlaying.class);
intentNotification.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

// Create the back stack so the user can get back to the main activity when pressing the back button
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(NowPlaying.class);
stackBuilder.addNextIntent(intentNotification);
PendingIntent pendingNowPlayingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

// Setup the custom notification view
RemoteViews notificationContent = new RemoteViews(getPackageName(), R.layout.notification_now_playing);
....

// Setup the intent that will play/stop music when the stop button is tapped
Intent musicControlIntent = new Intent(this, CommotionMediaPlayer.class);

PendingIntent musicPendingIntent = PendingIntent.getService(this, 0, musicControlIntent, PendingIntent.FLAG_UPDATE_CURRENT);

notificationContent.setOnClickPendingIntent(R.id.ibMediaControl, musicPendingIntent);

// Update the notification with this info
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle(song.text)
                    .setContentText(song.artist)
                    .setContentIntent(pendingNowPlayingIntent)
                    .setLargeIcon(artwork)
                    .setContent(notificationContent)
                    .setOngoing(true);

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// Using the nowPlayingNotificationId allows the notification to be updated with later calls instead of causing a new notification to show up
mNotificationManager.notify(nowPlayingNotificationId, builder.build());

Here's the applicable definitions in my AndroidManifest.xml (for the wrapper project):

    <activity android:name="com.be.commotion.ui.StartupActivity"
            android:label="@string/app_name"
            android:noHistory="true"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Holo.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity android:name="com.be.commotion.ui.NowPlaying"
              android:label="Now Playing"
              android:parentActivityName="com.be.commotion.ui.StartupActivity"
              android:screenOrientation="portrait">

        <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.be.commotion.ui.StartupActivity" />

    </activity>

Upvotes: 4

Views: 2795

Answers (3)

tsik
tsik

Reputation: 629

Maybe we are solving a slightly different problem but it boils down to the same issue. For us the solution was to use:

startActivityForResult(intent, 0);

What we wanted to do is to have a notification that takes to Activity B and, when the back button is clicked, to take us to Activity A

There are two ways to do that:

Start A from B: Works if all activities are in your app

  1. Send a parameter in the notification bundle to define that the activity was started from a notification
  2. Override the OnBack of activity B and make it start activity A with a CLEAR_TOP flag (so that the back does not take back to B)

Start B from A: Works for activities in different apps (e.g. opening an email client)

  1. Make the notification to take to Activity A (pass the necessary parameters to know that the activity was started from a notification)
  2. Start activity B (e.g. the email client) with a startActivityForResult. If you use the normal startActivity the "back" button will not work.

Upvotes: 0

Manan Sheth
Manan Sheth

Reputation: 316

Add the main activity to the stack before you add the desired activity(the activity directly called when notification is clicked). Eg:

stackBuilder.addNextIntent(new Intent(this,MainActivity.class));
stackBuilder.addNextIntent(new Intent(this,DesiredActivity.class));

Upvotes: 2

user1747373
user1747373

Reputation: 33

You can do it by 2 step without using TaskStackBuilder
1/ set Flag for your Intent

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);

2/set Flag for your PendingIntent

pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);

It works for me, good luck guys

Upvotes: 3

Related Questions