关闭并重新开启Android应用程序

时间:2015-05-28 16:09:22

标签: android

我正在制作一款包含两项活动的Android应用,主菜单和游戏活动。到目前为止,应用程序运行正常,并且在活动之间来回切换可以正常工作。当游戏活动正在运行并按下主页按钮时出现问题。当我点击图标重新打开游戏时,它会打开黑屏并且什么都不做。我每次调用一个生命周期方法和打印消息时都会创建它,但是当我尝试重新打开应用程序时,它不会为两个活动中的任何一个调用onCreate(),onStart()或onResume()。如果我去任务管理器并关闭应用程序,然后重新打开它,它会按预期打开主菜单活动。只有在我按下主页按钮然后再次尝试打开应用程序时才会出现此问题。有什么方法可以解决这个问题吗?

以下是我的游戏活动中的一些代码,包括所有生命周期方法:

public class Klondike extends Activity{

private KlondikeGameView gameView;
private StatsHandler statsHandler;
private KlondikeGameStats gameStats;
private RelativeLayout mainLayout;
public boolean createSavedGame;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    createSavedGame = true;
    setContentView(R.layout.klondike);
    mainLayout = (RelativeLayout) findViewById(R.id.main_klondike_layout);
    DisplayMetrics dm = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(dm);

    this.statsHandler = new StatsHandler();
    this.gameStats = new KlondikeGameStats(System.currentTimeMillis());

    gameView = new KlondikeGameView(getApplicationContext(), this, dm.heightPixels, dm.widthPixels, statsHandler, gameStats);
    mainLayout.addView(gameView);
    gameView.startGameThread();
}

@Override
protected void onStart() {
    super.onStart();
    System.out.println("OnStart was called for Klondike");
}

@Override
protected void onResume() {
    super.onResume();
    System.out.println("OnResume was called for Klondike");
    if (KlondikeSaveHandler.checkIfSavedStateExists(getApplicationContext())){
        KlondikeSaveHandler.restoreSavedState(getApplicationContext(), gameStats, gameView.getGameBoard());
    }
}

@Override
protected void onPause() {
    super.onPause();
    System.out.println("OnPause was called for Klondike");
    if (createSavedGame){
        KlondikeSaveHandler.prepareSavedGame(getApplicationContext(), gameStats, gameView.getGameBoard());
        System.out.println("Game saved");
    }
    else {
        System.out.println("Game not eligible for saving");
    }
}

@Override
protected void onStop() {
    super.onStop();
    System.out.println("OnStop was called for Klondike");
}

@Override
protected void onRestart() {
    super.onRestart();
    System.out.println("OnRestart was called for Klondike");
}

@Override
protected void onDestroy() {
    super.onDestroy();
    System.out.println("OnDestroy was called for Klondike");
}
}

点击主页按钮后,调用onPause,游戏保存正确。

这是清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.app.thomas.solitaire" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.app.thomas.solitaire.MainMenu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.app.thomas.solitaire.Klondike"
            android:label="Klondike"
            android:theme="@style/AppTheme">
        </activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

尝试放

gameView = new KlondikeGameView(getApplicationContext(), this, dm.heightPixels, dm.widthPixels, statsHandler, gameStats);
mainLayout.addView(gameView);
gameView.startGameThread();
onResume()中的

而不是onCreate()

相关问题