简体   繁体   中英

Closing and Reopening Android App

I'm making an Android app with two activities, a main menu and a game activity. So far the app runs fine, and switching back and forth between activities works as it should. A problem arises though when the game activity is running and I press the home button. When I tap the icon to reopen the game, it opens to a black screen and doesn't do anything. I created all of the life cycle methods and print messages each time one is called, but when I try to reopen the app, it doesn't call onCreate(), onStart(), or onResume() for either of the two activities. If I go to task manager and close the app, then go to reopen it, it opens up to the main menu activity as expected. The problem only arises when I hit the home button and then try to open the app again. Is there any way to fix this?

Here is some code from my game activity, including all the life cycle methods:

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");
}
}

After I hit the home button, onPause is called, and the game is saved correctly.

Here is the manifest file:

<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>

try putting

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

in onResume() than in onCreate()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM