我正在製作包含兩項活動,一個主菜單和一項遊戲活動的Android應用程序。到目前爲止,該應用運行良好,並且在各個活動之間來回切換,因爲它應該如此。雖然遊戲活動正在運行並且按下主頁按鈕,但會出現問題。當我點擊圖標重新打開遊戲時,它會打開黑屏,並且什麼也不做。我創建了所有生命週期方法並在每次調用時都打印消息,但是當我嘗試重新打開該應用程序時,它不會爲兩個活動中的任何一個調用onCreate(),onStart()或onResume()。如果我去任務管理器並關閉應用程序,然後重新打開它,它會按預期打開主菜單活動。只有當我點擊主頁按鈕然後嘗試再次打開應用程序時纔會出現問題。有沒有什麼辦法解決這一問題?關閉並重新打開Android應用程序
這裏是我的遊戲活動的一些代碼,包括所有的生命週期方法:
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>
你檢查,看看有什麼生命週期方法被調用你的時候打homebutton - 即onPause,onStop和可能onDestroy?如果您找到了按下主頁按鈕後該活動的狀態,它可以讓您瞭解爲什麼這些生命週期方法可能未被調用。此外,您的遊戲活動中的一些代碼可以幫助您完成活動的一般流程。 –
是的,當我點擊主頁按鈕時,onPause被稱爲 – Thomas
你可以發佈你的清單文件嗎? –