只需按一下即可清除一堆活动
我有一个启动 Activity
A1,它有一个启动按钮,可以启动 Service
S1:
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i(TAG1, "Starting Update Service");
startService(serviceIntentS1);
}
});
S1 根据某些条件启动 Activity
A2:
if (giveninteger>=2)
{
Intent intentA2= new Intent(this, A2.class);
// following line to avoid exception
intentA2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //to avoid exception
startActivity(intentA2);
}
A2 订阅S1和A2用户可以借助S1看到定期更新的数据。 A2 有以下代码来停止 S1 服务:
public void onBackPressed() {
try {
Log.i(TAG2, "Killing Update Service");
stopService(serviceIntentS1);
} catch (NullPointerException e) {
Log.i(TAG3, "Service was not running " + e.toString());
}
finish();
System.exit(0);
return;
}
我的问题是,如果更新从 A2 运行 10 次,用户必须按后退按钮 10 次才能退出 Activity
A2。也就是说A2的实例被累积在Activity
堆栈中。我在从 S1 启动 A2 期间尝试了所有标志,但没有成功。我想只需按一次后退即可退出 Activity
A2,无论更新运行多少次。
任何建议都会有所帮助。
I have a launching Activity
A1 which has a start button which starts a Service
S1:
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i(TAG1, "Starting Update Service");
startService(serviceIntentS1);
}
});
S1 depending on some condition starts Activity
A2:
if (giveninteger>=2)
{
Intent intentA2= new Intent(this, A2.class);
// following line to avoid exception
intentA2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //to avoid exception
startActivity(intentA2);
}
A2 subscribes to S1 and from A2 user can see periodically updated data by the aid of S1. A2 has following code to stop S1 service:
public void onBackPressed() {
try {
Log.i(TAG2, "Killing Update Service");
stopService(serviceIntentS1);
} catch (NullPointerException e) {
Log.i(TAG3, "Service was not running " + e.toString());
}
finish();
System.exit(0);
return;
}
My problem is that, if the update runs 10 times from A2, user has to press back button 10 times to exit Activity
A2. That is instances of A2 are accumulated in Activity
stack. I tried all flags during launch of A2 from S1, but without success. I want to exit the Activity
A2 with just one back press, no matter how many times the update runs.
Any suggestions would help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要的是 A2 的 SingleInstance,这样无论 A2 启动多少次,都只保留一个实例,并且您只需按一次后退按钮。
在 AndroidManifest 文件中定义此属性。
What you need is a SingleInstance of A2 so that irrespective of the number times A2 is launched only one instance remains and you need to press back button only once.
Define this attribute in the AndroidManifest file.