Android:执行 onPause 后按返回(和方向改变)会导致挂起
因此,我设法让我的程序在按下主页按钮时失去焦点,并在恢复时成功恢复活动,不幸的是,我使用的方法会导致线程在屏幕发生变化时挂起,而不会失去焦点立即地。
编辑--将 onResume 放入主 Activity 中
@Override
protected void onPause()
{
super.onPause();
synchronized (mPauseLock) {
mPaused = true;
}
return;
}
@Override
public void onResume() {
super.onResume();
synchronized (mPauseLock) {
mPaused = false;
mPauseLock.notifyAll();
}
}
public class ThreadClass extends Thread
{
boolean isRunning;
public void BeginThread()
{
if (mGameIsRunning) {
onResume();
}else{
if(isRunning)return;
isRunning=true;
start();
mGameIsRunning=true;
}
}
public void run()
{
Canvas canvas;
while(isRunning)
{
canvas=null;
try
{
canvas = canvasClass.surfaceHolder.lockCanvas(null);
synchronized (canvasClass.surfaceHolder)
{
//processClass.processGame();
canvasClass.onDraw(canvas);
}
}finally{
if(canvas!=null)
{
canvasClass.surfaceHolder.unlockCanvasAndPost(canvas);
}
}
synchronized (mPauseLock)
{
while (mPaused)
{
try
{
mPauseLock.wait();
} catch (InterruptedException e) {
}
}
}
}
}
}
必须更改什么才能使 Activity 在按 Home 时暂停并在按 Back 时被抛出堆栈?另外,正如所解释的,当屏幕方向改变时也会发生同样的问题。
So I manage to get my program to lose focus when pressing the Home button and successfully bring the activity back when resuming, unfortunately the method I'm using causes the Thread to hang when the screen changes in a matter that it doesn't lose focus immediately.
EDITTED -- Placed onResume into main Activity
@Override
protected void onPause()
{
super.onPause();
synchronized (mPauseLock) {
mPaused = true;
}
return;
}
@Override
public void onResume() {
super.onResume();
synchronized (mPauseLock) {
mPaused = false;
mPauseLock.notifyAll();
}
}
public class ThreadClass extends Thread
{
boolean isRunning;
public void BeginThread()
{
if (mGameIsRunning) {
onResume();
}else{
if(isRunning)return;
isRunning=true;
start();
mGameIsRunning=true;
}
}
public void run()
{
Canvas canvas;
while(isRunning)
{
canvas=null;
try
{
canvas = canvasClass.surfaceHolder.lockCanvas(null);
synchronized (canvasClass.surfaceHolder)
{
//processClass.processGame();
canvasClass.onDraw(canvas);
}
}finally{
if(canvas!=null)
{
canvasClass.surfaceHolder.unlockCanvasAndPost(canvas);
}
}
synchronized (mPauseLock)
{
while (mPaused)
{
try
{
mPauseLock.wait();
} catch (InterruptedException e) {
}
}
}
}
}
}
What has to be changed so that the activity will pause when pressing Home and be thrown out of the stack when pressing Back? Also as explained, the same issue happens when screen orientation changes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么 onResume 位于线程类内部?
如果这是实际代码,请将其删除到活动类中。
其余代码看起来不错。
Why does the onResume is inside the thread class?
If that's the actual code, remove it to the activity class.
Rest of the code looks fine.