문제

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

도움이 되었습니까?

해결책

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.

다른 팁

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(); 
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top