我的Android应用程序不会调用onResume

时间:2013-01-10 12:49:43

标签: android onresume

我正在开发一款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);
}}

谢谢,如果我的英语太糟糕,我很抱歉。

1 个答案:

答案 0 :(得分: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
}

试试这些。它可能对你有帮助。