Android:多个活动和 OnDestroy 问题

发布于 2024-09-26 02:30:42 字数 1098 浏览 5 评论 0原文

我有几十个活动:1 个主要活动、1 个动画、10 个其他活动以及更多活动的子集(几十个)。

从主窗口中,您可以通过按钮转到任何其他

其他调用子集活动,并且他们调用动画活动。

每个活动(包括子集和动画)都有一个返回主活动的按钮。

所有按钮都会引导用户纠正活动。

问题:我想通过使用设备的后退键退出主要活动。但是,发生的情况是它在之前的所有活动中倒退了。

“返回主”按钮(在每个活动中)都有 finishonDestroy 。所以,我不确定为什么这些屏幕/活动没有被破坏......?

任何评论/建议/澄清表示赞赏 - 谢谢

[添加代码片段]

注意:我移动/添加/删除了完成,onDestroy,onStop...尝试了很多方法,所以片段中显示的只是我尝试的一种方法...

    //  ---------------------------------------------------------
mainMenu.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // do something
        Intent Maincode = new Intent();
          Maincode.setClassName(
                              "com.bt.test",
                              "com.bt.test.Maincode");
        //  startActivity(Maincode); // go to maincode  
          finish();
          onStop();
        onDestroy();
        startActivity(Maincode); // go to maincode  
    }
}); // end -----------------------------------------------

I have a few dozen activities: 1-Main, 1-Animation, 10-Other's and it's sub-sets of more activities (couple of dozen).

From the Main, you can go to any of the Others via buttons.

The Others call sub-set activities and they call the Animation activity.

Each of the activities, including sub-sets and Animation, has a button to return to the Main activity.

All buttons take user to correct activity.

The issue: From the Main activity, I want to quit via using the device's Back key. But, what happens is that it steps backward through all the previous activities.

The "return to main" buttons (in each activity) has finish and onDestroy in it. So, I'm not sure why those screen/activities aren't destroyed...?

Any comments/suggestions/clarifications is appreciated - thanks

[adding code snippet]

Note: I moved/added/deleted the finish, onDestroy, onStop... tried many ways so what shows in the snippet is only one way I tried...

    //  ---------------------------------------------------------
mainMenu.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // do something
        Intent Maincode = new Intent();
          Maincode.setClassName(
                              "com.bt.test",
                              "com.bt.test.Maincode");
        //  startActivity(Maincode); // go to maincode  
          finish();
          onStop();
        onDestroy();
        startActivity(Maincode); // go to maincode  
    }
}); // end -----------------------------------------------

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

暖阳 2024-10-03 02:30:42

您可以发布您的 onClick 处理程序以返回主按钮吗?

你应该做这样的事情:

Intent i = new Intent(this, MainActivity.class);
startActivity(i);
finish();

编辑:
您还可以尝试设置此标志:

i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

如果接收活动已经位于堆栈中的某个位置,这应该清除调用活动和接收活动之间的活动堆栈。

Could you post your onClick handler for the return to main button?

You should be doing something like this:

Intent i = new Intent(this, MainActivity.class);
startActivity(i);
finish();

Edit:
You could also try setting this flag:

i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

This should clear the activity stack between the calling and receiving activity if the receiving activity is already in the stack somewhere.

温柔一刀 2024-10-03 02:30:42

首先,您通常不应该自己调用 onStoponDestroy(或者任何其他 Activity 生命周期方法)。 Android 会在 finish 后为您执行此操作,如果您自己执行此操作,可能会感到困惑。

其次,您的“返回主”侦听器根本不应该调用任何 startActivity。相反,如果您想清除活动堆栈,您应该在那里调用 finish 。如果您可能远离主活动并希望直接返回,则应在启动子活动时使用 startActivityForResult ,并设置一个结果 Intent,并在“主按钮”上附加 true 标志。然后,任何中间活动都将调用 onActivityResult,如果它们看到该标志,它们也可以立即完成,以便控制权返回到您的主活动。

编辑:实际上,使用 startActivityFLAG_ACTIVITY_CLEAR_TOP 是获得相同效果的更直接的方法。坚持下去。

First, you should generally never be calling onStop or onDestroy (or, for that matter, any of the other Activity lifecycle methods) yourself. Android will do that for you as a result of finish, and will probably get confused if you do it yourself.

Second, your "return to main" listener should not be calling any startActivity at all. Instead, if you want to clear the activity stack, you should just call finish there. If you might be several Activities away from main and want to return directly, you should use startActivityForResult when launching sub-activities, and set a result Intent with a true flag attached on 'main button'. Then, any intermediate activity will get onActivityResult called and if they see the flag they can immediately finish too so that control gets back to your main activity.

EDIT: actually, startActivity with FLAG_ACTIVITY_CLEAR_TOP is a much more straightforward way to get the same effect. Stick with that.

憧憬巴黎街头的黎明 2024-10-03 02:30:42

Finish 不仅仅“完成”您的应用程序/活动,因为文档声明

“当您的活动完成并应该关闭时调用此函数。ActivityResult 将传播回通过 onActivityResult() 启动您的任何人。”

换句话说,您对完成的调用将通过您之前参与的任何活动传播回来。

Finish does not just "finish" your application/activity, as the Documents state

"Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult()."

In other words your call to finish will propagate back through any activity you had previously been in.

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