我想在点击通知时将活动A重新排序到前面,并且只有一个活动A的实例。
例如:
目前,我通过此订单启动了3项活动:Main-> A-> B
现在我点击通知并启动活动A,
我希望堆栈的顺序应该是:Main-> B-> A
但是,我得到的是:Main-> A-> B-> A
做这些事情的适当方法是什么?
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
Common.onClick(this, view);
}
}
public class ActivityA extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
Common.onClick(this, view);
}
}
public class ActivityB extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
Common.onClick(this, view);
}
}
public class Common {
public static void onClick(Activity activity, View view) {
Intent intent;
switch (view.getId()) {
//new activity anyway
case R.id.btnA:
intent = new Intent(activity, ActivityA.class);
activity.startActivity(intent);
break;
case R.id.btnB:
intent = new Intent(activity, ActivityB.class);
activity.startActivity(intent);
break;
//reorder activity to front
case R.id.btnAF:
intent = new Intent(activity, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
activity.startActivity(intent);
break;
case R.id.btnBF:
intent = new Intent(activity, ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
activity.startActivity(intent);
break;
//new activity anyway through notification
case R.id.btnNA:
intent = new Intent(activity, ActivityA.class);
sendNotification(activity, intent, "New A", "New A");
break;
case R.id.btnNB:
intent = new Intent(activity, ActivityB.class);
sendNotification(activity, intent, "New B", "New B");
break;
//reorder activity to front through notification
case R.id.btnNAF:
intent = new Intent(activity, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
sendNotification(activity, intent, "A to Front", "A to Front");
break;
case R.id.btnNBF:
intent = new Intent(activity, ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
sendNotification(activity, intent, "B to Front", "B to Front");
break;
}
}
private static void sendNotification(Activity activity, Intent targetIntent, String title, String msg) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(activity)
.setSmallIcon(R.drawable.abc_ic_menu_copy_mtrl_am_alpha)
.setContentTitle(title)
.setContentText(msg);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(activity);
stackBuilder.addNextIntent(targetIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(123456789, mBuilder.build());
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Activity" />
<Button
android:id="@+id/btnA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="New A" />
<Button
android:id="@+id/btnB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="New B" />
<Button
android:id="@+id/btnAF"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="A to Front" />
<Button
android:id="@+id/btnBF"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="B to Front" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Notification" />
<Button
android:id="@+id/btnNA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="N New A" />
<Button
android:id="@+id/btnNB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="N New B" />
<Button
android:id="@+id/btnNAF"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="N A to Front" />
<Button
android:id="@+id/btnNBF"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="N B to Front" />
</LinearLayout>
</LinearLayout>