0

I'm stuck with some activity's flow issue. The desired behaviour is the following:

From time to time, the user receives a notification. When this notification is clicked, a new Activity is opened with some information in it. In this Activity, there's a button whose purpose is to redirect the user to another Activity where more detailed information is showed. When the user is in the details Activity and presses the back button (or the back button in the ActionBar) this one is closed and the Main Activity is showed (this one is different from the one I mentioned in first place).

Everything works fine except from the last part. When the user presses the back button the application is closed and it is showed the Home Screen. Why is that happening?

Here is my AndroidManifest.xml:

<activity
    android:name=".MainActivity">
</activity>

<activity
    android:name=".DetailActivity"
    android:label="@string/title_detail_activity"
    android:parentActivityName="solar.panik.MainActivity" >
    <!-- Parent activity meta-data to support 4.0 and lower -->
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="solar.panik.MainActivity" />
</activity>

<activity
    android:name=".NotificationActivity"
    android:theme="@style/NotificationActivity"      
    android:excludeFromRecents="true">
</activity>

Here is the onClick code for the button that starts the DetailActivity from the NotificationActivity:

Intent intent = new Intent(NotificationActivity.this, DetailActivity.class);
startActivity(intent);
finish();

Thanks in advance

Rafael Redrado
  • 493
  • 1
  • 6
  • 19

2 Answers2

1

When you start your app from something other than the launcher, you'll need to pass the back stack with your intent.

Android tutorial

Scroll down to Create Back Stack When Starting Activity().

So in your case:

// Intent for the activity to open when user selects the notification
Intent detailsIntent = new Intent(this, DetailActivity.class);

// Use TaskStackBuilder to build the back stack and get the PendingIntent
PendingIntent pendingIntent =
        TaskStackBuilder.create(this)
                        // add all of DetailActivity's parents to the stack,
                        // followed by DetailsActivity itself
                        .addNextIntentWithParentStack(detailsIntent)
                        .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pendingIntent);

Check out this answer.

OLD ANSWER BELOW

Make sure in your details activity that onBackPressed() method isn't overridden (or defined).

If that's not it, try adding this to your manifest and remove your current ".MainActivity" Activity and tags. (Or replace it with this)

    <activity 
        android:name="solar.panik.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>

You have to declare it as MAIN, so the Up button knows where to go. "Back" will take you to the next Activity up on the hierarchy.

Hope that helps.

Community
  • 1
  • 1
Garret
  • 1,137
  • 9
  • 17
  • I'm not overridding the onBackPressed() function. Actually there's another Activity declared as MAIN and that's my login activity. There it is checked whether the user is logged in or not. If he's logged in the user is taken to the main activity, and if not, the login process is started. – Rafael Redrado May 12 '14 at 23:06
  • You have two >> on this line: android:value="solar.panik.MainActivity" />> – Garret May 12 '14 at 23:15
  • Ups, yeah, but that was not causing any problems – Rafael Redrado May 12 '14 at 23:21
  • Your manifest looks ok. Pressing back in the Details activity *should* always take to you MainActivity.... It's a longshot, but I would put define the full name of your activities... android:name="solar.panik.MainActivity" and not rely on it resolving on its own where the .MainActivity is.... Looking at my code, that's what I've done. – Garret May 12 '14 at 23:25
  • Nah, that was not the problem either :S – Rafael Redrado May 12 '14 at 23:33
  • The cause of the problem is the multiple entry points of your app... There are tons of apps that have this, so the answer's gotta be out there... I'm going to do some searching. – Garret May 12 '14 at 23:34
  • Answer has been updated... I'm confident of this solution as it was directly addressed in a google tutorial on back stacks. – Garret May 13 '14 at 00:44
  • That's it! That's what I needed to make it work. Since the details activity is started from the notification activity, I don't need to get a `PendingIntent` or build a `Notification`. I just need to create the `TastStackBuilder` and call the function startActivities(). Thanks again! :) – Rafael Redrado May 13 '14 at 16:11
0

Do this in your detailed activity. The one you click the back button in.

@Override
public void onBackPressed() {
    Intent intent = new Intent(DetailActivity.this, MainActivity.class);
    startActivity(intent);
    finish(); 
}
JCodes13
  • 488
  • 5
  • 17
  • It should be: `Intent intent = new Intent(DetailActivity.this, MainActivity.class);` Because I'm trying to go back to the MainActivity from the DetailActivity, isn't it? But that code is not working properly. It keeps taking me to the Home Screen :/ – Rafael Redrado May 12 '14 at 22:40
  • What android version? – JCodes13 May 12 '14 at 22:44
  • I'm testing it in Android 4.2.1 – Rafael Redrado May 12 '14 at 22:46
  • I might say that it's not a problem with my MainActivity. It is possible to go to the detail activity directly from the main activity and then, the back button is working fine. But the problem is when I launch the detail activity from the notification activity. I don't know if you understand my problem. – Rafael Redrado May 12 '14 at 22:52
  • Try adding a button to your detail activity then adding the intent code in the onclick() – JCodes13 May 12 '14 at 22:57
  • I've tried it, and it works that way, but only if I delete the `moveTaskToBack(true);` that you added. The method onBackPressed you mentioned before is only executed when the physical back button is pressed, I also have a back button in the action bar, and that one is not working – Rafael Redrado May 12 '14 at 23:24