2017-02-19 69 views
-1

所以我的应用程序是这样的: mainActivity - >gameview>gamegeneration如何保存视图类的状态?

mainActivity将传递信息(上下文,attibutes)插入gameview这将通过gridColRow量成gamegeneration(生成随机网格),然后将被显示与gameview

但是,如果我改变屏幕方向一个新的网格被创建,我怎么得到它保持相同的网格,即使屏幕方向改变后?

我在gameview用这个,

protected Parcelable onSaveInstanceState() {} 

而且

protected void onRestoreInstanceState(Parcelable state) {} 

&束放/恢复。

然而它不工作,电网仍然不同。下面是 是一个简单的例子(我只是想存储一个数字,但仍然有问题)。

感谢

protected Parcelable onSaveInstanceState() { 
     Bundle bd = new Bundle(); 
     bd.putParcelable("instanceState", super.onSaveInstanceState()); 
     bd.putInt("save1", numbersave); 
     return super.onSaveInstanceState(); 

    } 

protected void onRestoreInstanceState(Parcelable state) { 
     Bundle bd = (Bundle) state; 
     super.onRestoreInstanceState(bd.getParcelable("instanceState")); 
     numbersave= bundle.getInt("save1"); 
    } 
+0

我不明白你是什么正试图在这里做!你是否试图传递你创建的某个类的对象,或者你试图保存活动状态并因此保存你的对象状态? –

+0

第二个,我需要保存对象状态,以便使用相同的网格,而不是重新创建。例如在我的游戏活动的oncreate,我有mView = new GameView(this,null); 。每次创建活动时都会调用它,并且新的网格被制作为 – confusedOne

+0

,那么[您要保存其对象状态的类]必须为'parcelable'或'serializable',然后使用我的答案来保存它的实例状态 –

回答

0

你需要重写onSaveInstanceState(Bundle savedInstanceState),写你想改变的Bundle参数这样的应用程序的状态值:

@Override 
public void onSaveInstanceState(Bundle savedInstanceState) { 
    super.onSaveInstanceState(savedInstanceState); 
    // Save UI state changes to the savedInstanceState. 
    // This bundle will be passed to onCreate if the process is 
    // killed and restarted. 
    savedInstanceState.putBoolean("MyBoolean", true); 
    savedInstanceState.putDouble("myDouble", 1.9); 
    savedInstanceState.putInt("MyInt", 1); 
    savedInstanceState.putString("MyString", "Welcome back to Android"); 
    // etc. 
    // Also you can add parcelable and serializable for Objects of classes 
} 

的包基本上是存储的一种方式NVP(“名称 - 值对”)地图,它将被传递到onCreate()即您可以恢复您的状态onCraete

// Check whether we're recreating a previously destroyed instance 
    if (savedInstanceState != null) { 
     // Restore UI state from the savedInstanceState. 
     // This bundle has also been passed to onCreate. 
     boolean myBoolean = savedInstanceState.getBoolean("MyBoolean"); 
     double myDouble = savedInstanceState.getDouble("myDouble"); 
     int myInt = savedInstanceState.getInt("MyInt"); 
     String myString = savedInstanceState.getString("MyString"); 
     // etc. 
     // Also you can get parcelable and serializable for Objects of classes 
    } else { 
     // Probably initialize members with default values for a new instance 
    } 

OR可以覆盖onRestoreInstanceState()在那里你会提取这样的价值观:

@Override 
public void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 
    // Restore UI state from the savedInstanceState. 
    // This bundle has also been passed to onCreate. 
    boolean myBoolean = savedInstanceState.getBoolean("MyBoolean"); 
    double myDouble = savedInstanceState.getDouble("myDouble"); 
    int myInt = savedInstanceState.getInt("MyInt"); 
    String myString = savedInstanceState.getString("MyString"); 
    // etc. 
    // Also you can get parcelable and serializable for Objects of classes 
} 

你通常会使用此技术来存储实例值的应用程序(选择,未保存的文字等)。

编辑

要保存Gameview类的对象的状态,那么该类必须实现parcelable(推荐)或serializable,看到这个answer的细节