-1

I have a ToDoItem.java class and in my mainActivity class i have an arraylist in type of ToDoItem. I mean i have a "ArrayList(ToDoItem) listItems". I want to protect my arraylist in case of any configuration change. For this reason i want to use onSaveInstanceState function but in outState.(...) part i couldn't find any suitable function for my arraylist type. Can someone help me about how to save my arraylist? Thank you in advance.

Here is the code that i tried:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if(savedInstanceState == null)
    {
        listItems = new ArrayList<ToDoItem>();
    }
    else
    {
        listItems = savedInstanceState.getStringArrayList("datas2");//problem in this line
    }

}

@Override
protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);

    outState.putStringArrayList("datas2", listItems); //problem in this line
}

logcat when i use outState.putSerializable("datas2", listItems):

11-12 17:44:44.565: E/AndroidRuntime(1321): FATAL EXCEPTION: main
11-12 17:44:44.565: E/AndroidRuntime(1321): java.lang.RuntimeException: Parcel: unable to marshal value 0   burcu   12-11-2013   NOT DONE
11-12 17:44:44.565: E/AndroidRuntime(1321):     at android.os.Parcel.writeValue(Parcel.java:1235)
11-12 17:44:44.565: E/AndroidRuntime(1321):     at android.os.Parcel.writeList(Parcel.java:622)
11-12 17:44:44.565: E/AndroidRuntime(1321):     at android.os.Parcel.writeValue(Parcel.java:1195)
11-12 17:44:44.565: E/AndroidRuntime(1321):     at android.os.Parcel.writeMapInternal(Parcel.java:591)
11-12 17:44:44.565: E/AndroidRuntime(1321):     at android.os.Bundle.writeToParcel(Bundle.java:1646)
11-12 17:44:44.565: E/AndroidRuntime(1321):     at android.os.Parcel.writeBundle(Parcel.java:605)
11-12 17:44:44.565: E/AndroidRuntime(1321):     at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2318)
11-12 17:44:44.565: E/AndroidRuntime(1321):     at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3100)
11-12 17:44:44.565: E/AndroidRuntime(1321):     at android.os.Handler.handleCallback(Handler.java:730)
11-12 17:44:44.565: E/AndroidRuntime(1321):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-12 17:44:44.565: E/AndroidRuntime(1321):     at android.os.Looper.loop(Looper.java:137)
11-12 17:44:44.565: E/AndroidRuntime(1321):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-12 17:44:44.565: E/AndroidRuntime(1321):     at java.lang.reflect.Method.invokeNative(Native Method)
11-12 17:44:44.565: E/AndroidRuntime(1321):     at java.lang.reflect.Method.invoke(Method.java:525)
11-12 17:44:44.565: E/AndroidRuntime(1321):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-12 17:44:44.565: E/AndroidRuntime(1321):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-12 17:44:44.565: E/AndroidRuntime(1321):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

1

你的ToDoItem班级必须实现 Parcelable

见文档

于 2013-11-12T23:06:01.193 回答
-2

你可以使用

@Override 
public Object onRetainNonConfigurationInstance() {
    return this.listItems;    
}

在 onCreate() 中:

public void onCreate(Bundle savedState)   {
   this.listItems= (List<ToDoItem>)getLastNonConfigurationInstance();

   if(this.listItems!= null) { 

   }
}

已弃用但工作...

如果您出于任何原因想要使用 savedInstanceState,您可以让您的对象实现 Parcelable ... 查看帖子:如何在 Android 屏幕旋转上保存自定义 ArrayList?

于 2013-11-12T22:29:21.560 回答