当其他一些活动到达前台时,如何保存当前活动的状态

时间:2013-09-12 20:45:01

标签: android android-activity state

我正在构建我的第一个Android应用程序,这是一个棋盘游戏,包括ImageViews(可拖动),ToggleButton(打开/关闭声音),TextViews(用于分数),ArrayList,布尔值,整数,字符串,浮点数和一些双打。如果我按回按钮进入主菜单,并尝试回到我正在玩的游戏,它会崩溃。我没有实现onPause,onStop,onRestart,onResume中的任何一个。我只实现了onCreate。我在这个网站上发了几篇文章,但所有答案都令人困惑。 Shoud我使用onPause和onResume或onSaveInstanceState和onRestoreInstanceState来保存我当前的游戏状态(所有数据类型和对象)。一个例子将非常感激。

提前致谢!!

我的Logcat

09-12 16:13:06.326: E/AndroidRuntime(14207): FATAL EXCEPTION: main
09-12 16:13:06.326: E/AndroidRuntime(14207): java.lang.RuntimeException: Unable to resume activity {com.example.baghchalNepal/com.example.baghchal.UserAsTiger}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
09-12 16:13:06.326: E/AndroidRuntime(14207):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2851)
09-12 16:13:06.326: E/AndroidRuntime(14207):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2880)
09-12 16:13:06.326: E/AndroidRuntime(14207):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2302)
09-12 16:13:06.326: E/AndroidRuntime(14207):    at android.app.ActivityThread.access$700(ActivityThread.java:152)
09-12 16:13:06.326: E/AndroidRuntime(14207):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
09-12 16:13:06.326: E/AndroidRuntime(14207):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-12 16:13:06.326: E/AndroidRuntime(14207):    at android.os.Looper.loop(Looper.java:137)
09-12 16:13:06.326: E/AndroidRuntime(14207):    at android.app.ActivityThread.main(ActivityThread.java:5328)
09-12 16:13:06.326: E/AndroidRuntime(14207):    at java.lang.reflect.Method.invokeNative(Native Method)
09-12 16:13:06.326: E/AndroidRuntime(14207):    at java.lang.reflect.Method.invoke(Method.java:511)
09-12 16:13:06.326: E/AndroidRuntime(14207):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
09-12 16:13:06.326: E/AndroidRuntime(14207):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
09-12 16:13:06.326: E/AndroidRuntime(14207):    at dalvik.system.NativeStart.main(Native Method)
09-12 16:13:06.326: E/AndroidRuntime(14207): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
09-12 16:13:06.326: E/AndroidRuntime(14207):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3435)
09-12 16:13:06.326: E/AndroidRuntime(14207):    at android.view.ViewGroup.addView(ViewGroup.java:3306)
09-12 16:13:06.326: E/AndroidRuntime(14207):    at android.view.ViewGroup.addView(ViewGroup.java:3251)
09-12 16:13:06.326: E/AndroidRuntime(14207):    at android.view.ViewGroup.addView(ViewGroup.java:3227)
09-12 16:13:06.326: E/AndroidRuntime(14207):    at com.example.baghchal.UserAsTiger.makeGoatMove(UserAsTiger.java:683)
09-12 16:13:06.326: E/AndroidRuntime(14207):    at com.example.baghchal.UserAsTiger.onResume(UserAsTiger.java:297)
09-12 16:13:06.326: E/AndroidRuntime(14207):    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1202)
09-12 16:13:06.326: E/AndroidRuntime(14207):    at android.app.Activity.performResume(Activity.java:5328)
09-12 16:13:06.326: E/AndroidRuntime(14207):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2841)
09-12 16:13:06.326: E/AndroidRuntime(14207):    ... 12 more

Java代码

    // create ImageView object to store a goat.
    ImageView thisGoat = null;          

    // This method call receives safest position where a goat can be placed.
    Point bestPoint = getBestPointToPlaceGoat();

    // if there are goats remained to be placed on the board, select a goat to be moved to the board.
    for (int i=0; i<imageArrayList.size(); i++) {
        if ((imageArrayList.get(i).getX() == (xMin+70)) &&  (imageArrayList.get(i).getY() == (yMin-200))){
            thisGoat = imageArrayList.get(i);
            break;
        }
    }

    // move the goat, if a goat was selected from above.
    if (thisGoat != null) {         

         // Here I am removing the ImageView obj
        relativeLayout.removeView(thisGoat);  

        thisGoat.setX(bestPoint.getX()-70);
        thisGoat.setY(bestPoint.getY()-70);
        bestPoint.setOccupiedBy("goat");            

         // this is where error is acc. to logcat
        relativeLayout.addView(thisGoat);           
    }

2 个答案:

答案 0 :(得分:0)

  

如果我按回按钮进入主菜单,并尝试回到我正在玩的游戏,它会崩溃。

如果唯一的问题是当您按下后退按钮时,如您所述,那么您可以覆盖后退按钮并在那里保存数据

@Override
public void onBackPressed()
{
    // save your data here using SharedPrefs, DB, or however you need
}

您不需要实施onResume(),因为Activity将被杀死,onCreate()将再次被调用,您可以检查存储的数据并恢复您的值。

如果您担心另一个Activity何时进入前台但当前Activity仍然在堆栈上,那么您可以以相同的方式覆盖onPause()并保存数据那个功能。

如果这些没有用,那么就像Prmths的评论中所述,发布你的logcat,这样我们就能看到错误是什么。

Activity Docs with lifecycle example

答案 1 :(得分:-1)

这些方法将为您使用

您可以使用 SharedPreferences 保存数据, onResume 将其恢复 从here

了解详情
@Override
protected void onResume()
{       
    super.onResume();
}
@Override
protected void onPause()
{
    super.onPause();
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
    // Save your data here
    super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
    super.onRestoreInstanceState(savedInstanceState);       
     // REtrieve your data heve
}

您应该了解有关Activity Life Cycle

的更多信息

enter image description here