Android使用onBackPressed()避免onDestroy() [英] Android avoiding onDestroy() with onBackPressed()

查看:578
本文介绍了Android使用onBackPressed()避免onDestroy()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一些应该很简单的事情.

I'm trying to do something that ought to be pretty simple.

我有一个游戏,该游戏可以通过一个提供各种选项的屏幕来启动StartActivity,其中之一是开始游戏".

I have a game which launches a StartActivity with a screen offering various options, one of which is "start game".

当用户选择此选项时,适当的游戏将以类似于以下代码的方式启动:

When the user chooses this option, the game proper is started with code akin to:

Intent i = new Intent();
i.setClass(context, GameActivity.class);
context.startActivity(i);

一切正常.在游戏中按下后退"按钮时,控件将返回到此StartActivity.再次,这很好.

This all works fine. When in the game the "back" button is pressed, control returns to this StartActivity. Again, this is fine.

但是,按后退"会导致"onDestroy()"被调用(根据文档)-这意味着当再次按下开始游戏"时,GameActivity从头开始.

However, pressing "back" causes "onDestroy()" to be called (as per the docs) - this means that when "start game" is pressed again, the GameActivity starts from scratch.

我想要的是当按下后退"按钮时将GameActivity保留在内存中,以便按下开始游戏"返回到上次停止的位置.

What I would like is for the GameActivity to be kept in memory when "back" is pressed, so that pressing "start game" returns to where it had left off.

几乎每个游戏都要做这件事,因此实际上必须非常简单明了,但是我不知道该怎么做.我一直试图以一种重载onBackPressed()的方式返回到StartActivity,但是没有成功;做类似的事情:

This is something nearly every game does, so must be pretty straightforward really, but I can't work out how to do it. I've been trying to overload onBackPressed() in such a way as to return to the StartActivity, but without success; doing something like:

public void onBackPressed() {
    Intent i = new Intent(this, StartActivity.class);
    startActivity(i);
}

表示堆栈永远增长,即A-> B-> A离开ABA而不仅仅是A.

means the stack grows eternally, ie A -> B -> A leaves ABA rather than just A.

我知道一般的活动流程(http://developer.android.com/reference/android/app/Activity.html).

I'm aware of the general activity flow (http://developer.android.com/reference/android/app/Activity.html).

我以错误的方式启动GameActivity吗?

Am I launching the GameActivity in the wrong way?

作为对wingman的回应,我知道各种活动状态,如果需要释放内存,可能会调用onDestroy().我确实在onPause()上保存了游戏状态,并在onResume()上加载了游戏,但是当时内存并不处于完整状态.

In response to wingman, I am aware of the various activity states, and that onDestroy() is likely to be called if required to free memory. I do save game state on onPause() and load on onResume(), but not the complete state the memory is in at that time.

如果我按"home",则该应用实际上处于最小化"状态,并返回到重新启动时的位置-不调用onDestroy().我正在尝试模拟这种行为,但是将应用程序返回到StartActivity而不是"home".

If I press "home" the app is "minimised" in effect, and returns to where it was when relaunched - onDestroy() is not called. I'm trying to emulate this behaviour but return the app to the StartActivity instead of "home".

就我所能从文档中收集到的信息(或至少是finish())而言,后退按钮始终会调用onDestroy().

The back button always calls onDestroy() as I far as I can gather from the docs (or finish() at least).

发生这种情况时,所有游戏都保存完整状态吗?我会很惊讶,但也许他们会这么做.

Do all games save a complete state when this happens? I'd be surprised, but maybe they do.

推荐答案

按下后退"按钮时,没有活动被完全销毁. Android会维护一堆正在运行的Activity.当用户按下后退按钮时,当前活动将从堆栈中弹出,并显示上一个活动.但是弹出的活动并没有被破坏,它仍在内存中.只有当系统内存不足时,它才会被销毁,然后,才会调用onDestroy().

No Activity is destroyed completely when back button is pressed. Android maintains a stack of running Activities. when user presses back button, the current activity is popped off the stack, and the previous one shows. But the popped off activity is not destroyed, its still in memory. It will be only destroyed when system runs low on memory, and only then, onDestroy() will be called.

在您的活动因用户回退而离开屏幕之前,android会调用您的活动的多种方法,您可以在其中保存其状态,并在用户再次导航到您的活动时将其恢复.在此处浏览活动生命周期文档和示例: http://developer.android. com/reference/android/app/Activity.html

Before your activity goes away from screen due to user pressing back, android calls a number of methods of your activity, where you can save its state, and restore it when user again navigates to your activity. Go through the Activity life cycle documentation and samples here : http://developer.android.com/reference/android/app/Activity.html

因此,将游戏活动状态保存在onPause()onStopped()方法之一上,在主菜单上,如果用户选择" Resume ",则从其保存状态开始游戏活动,否则,如果用户选择"新游戏",请从头开始游戏活动.

So, Save your game activity state on one of the onPause() or onStopped() methods, On the main menu, if user selects "Resume" , Start your game Activity from its saved state, else if user selects "New Game" , Start your game activity from scratch.

这篇关于Android使用onBackPressed()避免onDestroy()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆