[英]Putting and getting savedInstanceState data
我只是通過嘗試查找如何獲取其中的SomeIntegers g_values對象和ArrayList AnInteger對象的簡單示例,來了解Activity的生命周期。
實際上,它沒有多大意義,但可以作為我實際情況的范例,在該情況下,初始設置需要應用程序通過無數次預處理(例如訪問和列出字體),在APK中分析我所有可用的游戲來進行schlepp (在文件上以及在我的網站上在線記錄的球員記錄等)。最終的應用程序是一個游戲和活動系統,可以幫助各個年齡段的SpLD(閱讀障礙)學生鍛煉他們的閱讀,拼寫,組織能力和短期記憶。 這是認真的意圖。 盡管可以免費運行,但最好與SpLD主管/輔導員一起使用,他們可以設置負責的工作時間表,甚至添加自己的游戲。
無論如何,無關緊要的背景。
我可以使用對saveInstanceState的訪問來保存我有些復雜的對象(由於沒有正確形式的putxxxxx方法而使它們受阻)還是應該放棄這種方法並從持久性文件或數據庫中恢復數據? 可以希望在這個簡單示例的范圍內對此進行討論,而實際情況更是如此,只是細節不同。
注意后添加。 還有一個問題,當應用程序需要保存其InstanceState時,會將用戶/播放器帶回他/她所在的位置。 由於主要的影響似乎是平板電腦的方向,我也許可以通過在啟動時鎖定方向來避開它。 這樣也可以簡化許多顯示問題,但這是“不可接受的”樣式嗎?
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class TestBundle extends AppCompatActivity {
SomeIntegers g_values;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("onCreate (" + (savedInstanceState == null ? "null)" : "set)"));
if (savedInstanceState == null)
{ g_values = new SomeIntegers();
String result = g_values.report();
System.out.println("Startup Result: " + result);
setContentView(R.layout.activity_test_bundle); // Where do I put this line?
}
else
{ //Do I get g_values back here?
//More relevantly, can I, and how can I, put g_values in the
//savedInstanceState when onSaveInstanceState is called?
String result = g_values.report();
System.out.println("Result: " + result);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
System.out.println("onSaveInstanceState (" + (outState == null ? "null)" : "set)"));
//How do I add g_values to the Bundle?
}
// Following is just stuff to watch the progress of the
// Activity in the ADB Log. Not of much relevance. Or is it?
@Override
protected void onStop() {
super.onStop();
System.out.println("onStop()");
}
@Override
protected void onStart() {
super.onStart();
System.out.println("onStart()");
}
@Override
protected void onRestart() {
super.onRestart();
System.out.println("onRestart()");
}
@Override
protected void onResume() {
super.onResume();
System.out.println("onResume()");
}
@Override
protected void onPause() {
super.onPause();
System.out.println("onPause()");
}
@Override
protected void onDestroy() {
super.onDestroy();
System.out.println("onDestroy()");
}
}
public class SomeIntegers {
private ArrayList<AnInteger> c_values;
SomeIntegers() {
c_values = new ArrayList<AnInteger>();
c_values.add (new AnInteger(1));
c_values.add (new AnInteger(2));
c_values.add (new AnInteger(3));
c_values.add (new AnInteger(4));
c_values.add (new AnInteger(29));
c_values.add (new AnInteger(30));
}
String report() {
String g = "";
for (AnInteger ai : c_values) {
if (!g.isEmpty()) g = g + ", ";
g = g + ai.getC_value();
}
return (g.isEmpty() ? "Empty" : g);
}
}
public class AnInteger {
private int c_value;
AnInteger(int value) { c_value = value); }
public int getC_value () { return c_value; }
}
謝謝。 喬西·希爾
恢復活動狀態的概念基於設備方向。 因此,例如,如果您從持久性文件中提取一些更改,然后將其加載,則當屏幕更改其旋轉角度時,將重新創建數據。 因此,活動使用捆綁包包裝該數據,並允許用戶保存該文件的當前工作狀態,然后可以將其還原。 這是一個很好的鏈接 。 關於數據更改,您的要求聽起來是一致的。根據我關於預期文件大小的第一個問題,您的要求聽起來相對較小。
要處理復合數據類型和抽象數據類型,請考慮使用GSON.GSON。這是一個Java序列化/反序列化庫,可以將Java對象轉換為JSON並返回
因此,我建議您使用android中共享首選項的功能。如果您要保存的鍵值集合相對較小,則應使用SharedPreferences API。 一個SharedPreferences對象指向一個包含鍵-值對的文件,並提供讀取和寫入它們的簡單方法。 簡而言之,共享首選項允許您以鍵,值對的形式保存和檢索數據。
Android提供了多種存儲應用程序數據的方式。 如果您的需求需要存儲一致性,那么我會采用數據庫方法,建議使用realm。Realm是移動數據庫,是SQLite的替代品。 盡管是OO數據庫,但它與其他數據庫有一些區別。 Realm並未使用SQLite作為引擎。 相反,它具有自己的C ++內核,旨在提供SQLite的移動優先方法。
希望這對您有所幫助:)
首先,使您的數據模型實現Parcelable :
AnInteger:
public class AnInteger implements Parcelable {
private int c_value;
public AnInteger(int value) {
this.c_value = value;
}
public int getC_value() {
return c_value;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.c_value);
}
protected AnInteger(Parcel in) {
this.c_value = in.readInt();
}
public static final Parcelable.Creator<AnInteger> CREATOR = new Parcelable.Creator<AnInteger>() {
@Override
public AnInteger createFromParcel(Parcel source) {
return new AnInteger(source);
}
@Override
public AnInteger[] newArray(int size) {
return new AnInteger[size];
}
};
}
SomeIntegers:
public class SomeIntegers implements Parcelable {
private ArrayList<AnInteger> c_values;
public SomeIntegers() {
c_values = new ArrayList<>();
c_values.add(new AnInteger(1));
c_values.add(new AnInteger(2));
c_values.add(new AnInteger(3));
c_values.add(new AnInteger(4));
c_values.add(new AnInteger(29));
c_values.add(new AnInteger(30));
}
public String report() {
String g = "";
for (AnInteger ai : c_values) {
if (!g.isEmpty()) {
g = g + ", ";
}
g = g + ai.getC_value();
}
return (g.isEmpty() ? "Empty" : g);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeTypedList(this.c_values);
}
protected SomeIntegers(Parcel in) {
this.c_values = in.createTypedArrayList(AnInteger.CREATOR);
}
public static final Parcelable.Creator<SomeIntegers> CREATOR = new Parcelable.Creator<SomeIntegers>() {
@Override
public SomeIntegers createFromParcel(Parcel source) {
return new SomeIntegers(source);
}
@Override
public SomeIntegers[] newArray(int size) {
return new SomeIntegers[size];
}
};
}
然后,在活動中保存和恢復變得非常容易,這是一個使用當前數據模型的示例:
//set up class fields/members
private final static String STATE_G_VALS = "STATE_G_VALS";
SomeIntegers g_values = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_bundle);
System.out.println("onCreate (" + (savedInstanceState == null ? "null)" : "set)"));
if (savedInstanceState != null) {
// get g_values back here
g_values = savedInstanceState.getParcelable(STATE_G_VALS);
}
if (g_values == null) {
// ok its null, lets make one
g_values = new SomeIntegers();
}
// log some stuff
String result = g_values.report();
System.out.println("Result: " + result);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
//set g_values to the Bundle/saved state (even if it is null)
outState.putParcelable(STATE_G_VALS, g_values);
super.onSaveInstanceState(outState);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.