Android:为startActivity标记\u活动\u重新排序\u到\u前台工作,但不为通知工作
我想在单击通知时将活动A重新排序到前台,并且只有活动A的一个实例。Android:为startActivity标记\u活动\u重新排序\u到\u前台工作,但不为通知工作,android,android-intent,android-activity,notifications,android-pendingintent,Android,Android Intent,Android Activity,Notifications,Android Pendingintent,我想在单击通知时将活动A重新排序到前台,并且只有活动A的一个实例。 例如: 目前我有3项活动是通过这个订单发起的:Main->A->B 现在我单击通知并启动活动A, 我希望堆栈的顺序应该是:Main->B->A 然而,我得到的是:Main->A->B->A 做这件事的合适方法是什么 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle save
例如:
目前我有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);
}
}
公共类公共类{
公共静态void onClick(活动、视图){
意图;
开关(view.getId()){
//反正是新活动
案例R.id.btnA:
意向=新意向(活动,ActivityA.class);
活动。开始触觉(意图);
打破
案例R.id.btnB:
意向=新意向(活动,活动B.class);
活动。开始触觉(意图);
打破
//将活动重新排序到前端
案例R.id.btnAF:
意向=新意向(活动,ActivityA.class);
intent.addFlags(intent.FLAG\u ACTIVITY\u REORDER\u TO\u FRONT);
活动。开始触觉(意图);
打破
案例R.id.btnBF:
意向=新意向(活动,活动B.class);
intent.addFlags(intent.FLAG\u ACTIVITY\u REORDER\u TO\u FRONT);
活动。开始触觉(意图);
打破
//通过通知创建新活动
案例R.id.btnNA:
意向=新意向(活动,ActivityA.class);
发送通知(活动、意图、“新A”、“新A”);
打破
案例R.id.btnNB:
意向=新意向(活动,活动B.class);
发送通知(活动、意图、“新B”、“新B”);
打破
//通过通知将活动重新排序到前台
案例R.id.btnNAF:
意向=新意向(活动,ActivityA.class);
intent.addFlags(intent.FLAG\u ACTIVITY\u REORDER\u TO\u FRONT);
发送通知(活动、意图、“A到前台”、“A到前台”);
打破
案例R.id.btnNBF:
意向=新意向(活动,活动B.class);
intent.addFlags(intent.FLAG\u ACTIVITY\u REORDER\u TO\u FRONT);
发送通知(活动、意图、“B到前面”、“B到前面”);
打破
}
}
私有静态void sendNotification(活动活动、意图目标内容、字符串标题、字符串消息){
通知相容建筑商mBuilder=
新建NotificationCompat.Builder(活动)
.setSmallIcon(R.drawable.abc\U ic\U菜单\U复制\U mtrl\U am\U alpha)
.setContentTitle(标题)
.setContentText(msg);
TaskStackBuilder stackBuilder=TaskStackBuilder.create(活动);
stackBuilder.addNextIntent(targetIntent);
悬而未决的结果悬而未决的结果=
stackBuilder.getpendingent(
0,
PendingEvent.FLAG_更新_当前
);
mBuilder.setContentIntent(resultPendingContent);
通知经理通知经理=
(NotificationManager)activity.getSystemService(Context.NOTIFICATION\u服务);
mNotificationManager.notify(123456789,mBuilder.build());
}
}
@Francesc情况不一样。该帖子来自A->B,然后是主页->A@Francesc还有,那篇文章使用了startActivity,但我想要的是通过通知触发。你找到解决方案了吗?我刚刚遇到了这个问题。我正在做的是创建一个具有透明背景的虚拟活动。因此,当用户单击通知时,它将显示活动,并且所有处理都在该活动中完成,当然,在那里标记“活动”\u“重新排序”\u到\u front将正常。这个用户不会看到任何差异。您只需在该虚拟活动的onCreate
中调用finish()
。@Francesc情况不同。该帖子来自A->B,然后是主页->A@Francesc还有,那篇文章使用了startActivity,但我想要的是通过通知触发。你找到解决方案了吗?我刚刚遇到了这个问题。我正在做的是创建一个具有透明背景的虚拟活动。因此,当用户单击通知时,它将显示活动,并且所有处理都在该活动中完成,当然,在那里标记“活动”\u“重新排序”\u到\u front将正常。这个用户不会看到任何差异。您只需要在该虚拟活动的onCreate
中调用finish()
。
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>