这是我的活动:
package com.workspace.pockethero;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
public class PocketHero extends Activity {
/** Called when the activity is first created. */
private static final String TAG = PocketHero.class.getSimpleName();
static int centreX;
static int centreY;
static int bottomA;
@Override
public void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);//fullscreen
setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//landscape mode
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//keep screen on
requestWindowFeature(Window.FEATURE_NO_TITLE);//no title
super.onCreate(savedInstanceState);
Display display = getWindowManager().getDefaultDisplay();
centreX = display.getWidth()/2;
centreY = display.getHeight()/2;
bottomA = display.getHeight();
// requesting to turn the title OFF
// making it full screen
// set our MainGamePanel as the View
setContentView(new MainGamePanel(this));
Log.d(TAG, "View added");
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//replaces the default 'Back' button action
if(keyCode==KeyEvent.KEYCODE_HOME)
{
}
return true;
}
@Override
protected void onDestroy() {
Log.d(TAG, "Destroying...");
super.onDestroy();
}
@Override
protected void onStop() {
Log.d(TAG, "Stopping...");
super.onStop();
}
/** Called when the activity is first created. */
}
这项活动真是一团糟。我希望我的活动在最小化游戏时保存自己,并在我再次启动它时自我恢复。我应该在此活动中添加或删除什么?我是android开发的新手,所以我不知道这个活动还需要什么......
答案 0 :(得分:1)
如果您希望在应用程序的执行之间保存游戏状态,那么您不希望看到保存实例状态。相反,您希望将数据保存在某种形式的持久存储中。在app启动时(在onCreate
中),您将读取持久数据(或者如果没有找到则初始化默认数据 - 与第一次执行时一样)。指南主题Data Storage中描述了保存数据的选项。
答案 1 :(得分:0)
您需要使用序列化。将您的数据(centreX ...)包装在Serializable类中,将其保存在onDestroy()中并将其加载到onStart()中。
以下是我整天使用的两种便捷方法:
public static Object load(String fname , Context cont)
{
try
{
FileInputStream fis = cont.openFileInput(fname);
ObjectInputStream ois = new ObjectInputStream(fis);
Object loadedObject = ois.readObject();
ois.close();
fis.close();
return loadedObject;
}
catch(Exception e)
{
Father.print("[IO.load]Load "+fname+" failed: "+ e.getMessage() );
return null;
}
}
public static boolean save(String fname , Object o , Context cont)
{
try
{
if(o == null) Father.print("WARNING: saving null");
FileOutputStream fos = cont.openFileOutput(fname , Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(o);
oos.close();
fos.close();
return true;
}
catch(Exception e)
{
Father.printe("[IO.save]Error saving object: ", e );
return false;
}
}