如何在onSaveInstanceState()中存储值并检索?

时间:2013-06-19 05:23:41

标签: android android-arrayadapter

我试图在onSavedInstanceState()中存储一个arraylist的值,并在onCreate方法中获取数组列表中的值,但它会引发一个像无法暂停活动一样的英雄

这是我的代码

public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putParcelable("Old", (Parcelable) profileDetails );
    super.onSaveInstanceState(savedInstanceState);
}

并在我的onCreate()

if (savedInstanceState != null) { 
    profileDetails= (ArrayList<ProfileDetails>)savedInstanceState.getParcelable("Old");
}
else {
    profileDetails = GetSearchResults();
}

3 个答案:

答案 0 :(得分:6)

Bundle也可以存储Serializable个对象。

确保胜过class ProfileDetails implements Serializable

之后,您将能够使用以下方法在Bundle中保存/恢复ArrayList:

savedInstanseState.putSerializable("Old", profileDetails);
...
profileDetails = (ArrayList<ProfileDetails>)savedInstanceState.getSerializable("Old");

此外,您应该保证class ProfileDetails仅包含基本类型的字段或Serializable(字符串,数组已经实现此接口)。

答案 1 :(得分:5)

在保存实例状态中保存值

@Override
public void onSaveInstanceState(Bundle 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.
  super.onSaveInstanceState(savedInstanceState);
}

恢复价值

@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");
  }

答案 2 :(得分:4)

你不能将arraylist级联到parcelable这个链接可以帮助你获得parcel arraylist Android ArrayList<MyObject> pass as parcelable 这段代码可以帮到你

public class ParcleListTopic implements Parcelable{
    private List<ParcleTopic> list;
    private ArrayList<HoldListTopic> listh=new ArrayList<HoldListTopic>();
    public ArrayList<HoldListTopic> GetListTopic()
    {
        for(int i=0;i<list.size();i++)
        {
            listh.add(list.get(i).GetHold());
        }
        return listh;
    }
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeTypedList(list);
    }
    public ParcleListTopic(Parcel in)
    {
        in.readTypedList(list,ParcleTopic.CREATOR);

    }
    public ParcleListTopic(List<ParcleTopic> list)
    {
        this.list=list;
    }
    public static final Parcelable.Creator<ParcleListTopic> CREATOR = new Parcelable.Creator<ParcleListTopic>(){
          public ParcleListTopic createFromParcel(Parcel s)
          {
              return new ParcleListTopic(s);
          }
          public ParcleListTopic[] newArray(int size) 
          {
                return new ParcleListTopic[size];
          }
    };
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
}


public class ParcleTopic implements Parcelable
{
    HoldListTopic hold;
    public ParcleTopic(HoldListTopic hold)
    {
        this.hold=hold;
    }
    public HoldListTopic GetHold()
    {
        return hold;
    }
    public int describeContents() 
    {
        return 0;
    }
    public void writeToParcel(Parcel dest, int flags)
    {
        dest.writeString(hold.Title);
        dest.writeString(hold.Date);
        dest.writeInt(hold.NumberComment);
        dest.writeInt(hold.ID);
    }
    public ParcleTopic(Parcel in)
    {
        hold.Title=in.readString();
        hold.Date=in.readString();
        hold.NumberComment=in.readInt();
        hold.ID=in.readInt();
    }
    public static final Parcelable.Creator<ParcleTopic> CREATOR = new Parcelable.Creator<ParcleTopic>()
    {
          public ParcleTopic createFromParcel(Parcel s)
          {
              return new ParcleTopic(s);
          }
          public ParcleTopic[] newArray(int size) 
          {
                return new ParcleTopic[size];
          }
    }; }