0

我正在用 android 开发一个游戏,我有一个额外的错误。当我第一次执行应用程序时,一切正常,但是当我按下主页按钮然后我返回应用程序时,屏幕仍然是白色的,使用 Log 我可以看到 onResume() 没有被调用。当我按下主页按钮时,会调用 onPause() 方法。

public class MainActivity extends Activity {
GameView gV;
public static final String PREFS_NAME = "ShNCoPrefs";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    gV=new GameView(this);
    setContentView(gV);
    this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
    // Restore preferences
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    boolean isFirstPlay = settings.getBoolean("isFirstPlay", true);
    gV.setFirstPlay(isFirstPlay);



}

@Override
public void onResume(){
    super.onResume();
    Log.i("onResume", "main-pasó por aquí 1");
    if (gV.thread!=null)
    gV.runThread();
    Log.i("onResume", "main-pasó por aquí 2");


}

@Override
public void onPause(){
    super.onPause();
     SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
     SharedPreferences.Editor editor = settings.edit();
     editor.putBoolean("isFirstPlay", gV.isFirstPlay());

  // Commit the edits!
     editor.commit();
  if(gV.thread!=null)
     gV.pauseThread();

    //System.exit(0);

}


@Override
public boolean onKeyUp(int keyCode, KeyEvent event){
    switch (keyCode) {
    case KeyEvent.KEYCODE_BACK:
        return true;


    }
    return super.onKeyUp(keyCode, event);
}}

谢谢,如果我的英语这么差,我很抱歉。

4

1 回答 1

1
private boolean isActive = false;
@Override
public void onResume() {
   super.onResume();
   isActive = true;
}

@Override
pulic void onPause() {
  super.onPause();
  isActive = false;

}

//Do it if is not active
if(!isActive){
   //Operation you want to Perform
}

试试这些。它可能会帮助你。

于 2013-01-10T13:04:50.103 回答