只需按一下即可清除一堆活动

发布于 2024-12-17 21:08:44 字数 1306 浏览 0 评论 0原文

我有一个启动 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你对谁都笑 2024-12-24 21:08:45

您需要的是 A2 的 SingleInstance,这样无论 A2 启动多少次,都只保留一个实例,并且您只需按一次后退按钮。
在 AndroidManifest 文件中定义此属性。

<activity android:launchMode"singleInstance"/>

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.

<activity android:launchMode"singleInstance"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文