开始第二项活动,但未能回到第一项活动

时间:2014-10-17 09:37:40

标签: android activity-stack

假设我完全是我的应用程序。 (没有更多的活动堆栈)

但是,通知到达,用户将在点击

时导航到第二个活动

问题是如何将第二个活动转回第一个活动(lancher)然后退出

而不是直接退出,如果按下后退按钮(因为它没有任何堆栈)!?

导致第二次行动的通知。

 NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_stat_social_person)
                        .setContentTitle("AIR° TRACKING MODE!")
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(msg))
                        .setContentText(msg)
                        .setLights(Color.BLUE, 500, 500)
                        .setDefaults(Notification.DEFAULT_ALL)
                        .setOngoing(true);

        PendingIntent contentIntent = PendingIntent.getActivity(
                this,
                0,
                new Intent(this, Activity_Second.class).addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT),
                PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(GM.NOTIFICATION_TRACKING_MODE, mBuilder.build());
    }

5 个答案:

答案 0 :(得分:1)

试试这个..

您可以在onBackPressed()覆盖方法中添加Activity_Second.classActivity_First.class

@Override
public void onBackPressed() {
    Intent ide = new Intent(this, Activity_First.class);
    ide.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(ide);

    finish();
}

答案 1 :(得分:1)

TaskStackBuilder是最好的选择,我想是这样......

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(Activity_One.class);
    stackBuilder.addNextIntent(resultIntent);

并在清单中:

适用于API< 16

 <activity android:name=".Activity_Second">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".Activity_One" />
    </activity>

for API&gt; 16

 <activity android:name=".Activity_Second"
        android:parentActivityName=".Activity_One" />

对不起。当使用TaskStackBuilder时,记得像这样编辑PendingIntent:

     PendingIntent contentIntent =
        stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
        );

答案 2 :(得分:0)

使用 TaskStackBuilder

阅读此document

答案 3 :(得分:0)

使用此代码

@Override
public void onBackPressed() {
    Intent ide = new Intent(this, Activity_First.class);
    ide.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(ide);
    finish();
}

答案 4 :(得分:0)

TaskStackBuilder最适合复杂活动 易于管理......如果你知道如何!

从通知中导航代码&amp;添加适当的后筹码

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_stat_social_person)
                    .setContentTitle("AIR° TRACKING MODE!")
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(msg))
                    .setContentText(msg)
                    .setLights(Color.BLUE, 500, 500)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setOngoing(true);

    PendingIntent contentIntent = TaskStackBuilder.create(this)
            //addParentStack is destination class not its parent class
            //it will refer parent class from what you define in XML
            .addParentStack(Activity_TrackingMode.class)
            //and provide intent for destination class
            .addNextIntent(new Intent(this, Activity_TrackingMode.class))
            .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(GM.NOTIFICATION_TRACKING_MODE, mBuilder.build());

AndroidManifest中的XML示例

    <activity android:name=".Activity_TrackingMode"
              android:label="@string/app_name"
              android:screenOrientation="portrait"
              android:parentActivityName=".Activity_Screen1">
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".Activity_Screen1" />
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>

PS。&gt; import android.support.v4.app.TaskStackBuilder; //适用于16以下的支持API