简体   繁体   English

关闭并重新打开Android App

[英]Closing and Reopening Android App

I'm making an Android app with two activities, a main menu and a game activity. 我正在制作一个包含两个活动的Android应用程序,一个主菜单和一个游戏活动。 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. 我创建了所有生命周期方法并在每次调用一个方法时打印消息,但是当我尝试重新打开该应用程序时,这两个活动中的任何一个都不会调用onCreate(),onStart()或onResume()。 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. 在按下主屏幕按钮后,将调用onPause,并且游戏将正确保存。

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() 在onResume()中比在onCreate()中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:应用程式在关闭并重新开启后当机 - Android: App crashes after closing and reopening ANDROID:带有标签的应用程序,用于显示列表中项目的数量,仅在关闭并重新打开应用程序时才会更新 - ANDROID:app with tab which shows the count of number of items in the list gets updated only when closing and reopening the app Android:带有标签的应用程序,只有在关闭并重新打开应用程序后才能查看数据库更改 - Android: app with tab, database changes can be viewed only when closing and reopening app 关闭并重新打开应用程序后,CountDownTimer重新启动 - CountDownTimer getting restarted after closing and reopening the app 保存当前 ImageView state 用于关闭和重新打开应用程序 - Saving current ImageView state for closing and reopening a app Android:如何保存多个选中的复选框状态,甚至关闭并重新打开应用程序 - Android:How to save multiple checked checkbox state even closing and reopening the app 按下返回按钮关闭后,空白活动或重新打开应用程序上的片段 - Blank activity or fragment on reopening app after closing by pressing back button Dart/Flutter App 仅在模拟器中关闭和重新打开时抛出异常 - Dart/Flutter App only throws Exception when Closing and Reopening in Emulator 重复事件 - 关闭/重新打开应用程序时广播事件的多个问题 - Duplicated Event - Multiple issues of Broadcast event when closing/reopening app Android应用重新打开后显示白屏 - Android app shows white screen after reopening
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM