-1

我有一些关于通知意图的问题。我有 2 项活动(A 和 B)。活动 A 是我的应用程序的主要活动。用户将经历的第一个活动。至于活动 B 是当用户通过单击活动 A 处的按钮进入时。

问题是当我单击通知时,它会将我引导至活动 B。然后将我引导回活动 A onBackPressed。但是当我关闭应用程序并通过多任务选项打开它时,它会在 Activity B 处恢复我的应用程序。我希望应用程序在关闭它之后从 Activity A 重新开始。

顺序应该是

活动 A --> 活动 B。

通知onClick -> 活动 B ( onBackPressed ) -> 活动 A -> 关闭。

重新打开应用程序/使用多任务功能打开 --> Activity A

请让我知道是否有任何其他信息可以提供以正确理解我的问题。

GCM_Intent.class

    Notification note = new Notification(icon, msgTopic ,System.currentTimeMillis());
    Intent i=new Intent(this, Activity_B.class);
    i.putExtra("topicId", topicId);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
    Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pi=PendingIntent.getActivity(this, CommunitiesappConstant.NOTIFICATION_ID, i,  PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
    note.setLatestEventInfo(this, topicName ,msgInfo+message, pi);
    note.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
    note.ledARGB |= 0xff0000ff;
    note.ledOffMS |= 1000;
    note.ledOnMS |= 300;


    mgr.notify(CommunitiesappConstant.NOTIFICATION_ID, note);

活动B.class

@Override
public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_b);
    Bundle bundle = getIntent().getExtras();
    topicId = bundle.getString("topicId");

}

@Override
public void onBackPressed() {
    super.onBackPressed();
    Log.e(TAG,  "onBackPressed");
    Intent i = new Intent(Activity_B.this, Activity_A.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
    finish();
}
4

2 回答 2

1

通知是您可以在应用程序的正常 UI 之外向用户显示的消息。这是使用通知的完美代码。

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);

// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
        );
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
于 2013-10-08T04:16:35.140 回答
0

像这样 onback press(),

Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1));
        current.finish()
于 2013-10-08T05:23:26.790 回答