保存和恢复活动的状态 - Bundle始终为null?

时间:2016-04-15 12:52:38

标签: android android-activity bundle oncreate

我知道这个问题随处可见,而且我已经尝试过查看这样的答案:onSaveInstanceState not working,因为它与我的相似,但我无法找到解决方案。

这是我的代码:

public class SelectCamera extends AppCompatActivity {
static final String LIST_SAVE = "CAM-LIST";
String testString = "";
String testString02 = "please work";
private static final String TAG = "SELECTCAMERA ";
IPCamera ic = new IPCamera("Demo cam", "http://trackfield.webcam.oregonstate.edu/axis-cgi/mjpg/video.cgi?resolution=800x600&amp%3bdummy=1333689998337", "admin1", "password", this);
IPCamera testc = new IPCamera("TEST", "test.com", "admin1", "password", this);
static ArrayList<IPCamera> listOfCameras = new ArrayList<IPCamera>(); //Arrays.asList(ic)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
        System.out.println("WHAT IS SAVED: " + savedInstanceState.toString());
    } catch (NullPointerException e){
        System.out.println("NOTHING IS HERE ATM");
    }
    if (savedInstanceState != null) {
        System.out.println("THERE IS A SAVE --------------------");
        //listOfCameras.clear();
        listOfCameras = savedInstanceState.getParcelableArrayList(LIST_SAVE);
        testString = savedInstanceState.getString("test");
    } else {
        System.out.println("I am an IDIOT.....");
        listOfCameras.add(ic);
        listOfCameras.add(testc);
    }

    System.out.println("DOES THIS WORK: " + testString);
    setContentView(R.layout.select_camera);

    final ListView lv = (ListView) findViewById(R.id.cameraList);

    camListAdapter adapter = new camListAdapter(this, listOfCameras);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            IPCamera listItem = (IPCamera) lv.getItemAtPosition(position);

            Toast.makeText(getApplicationContext(), "Camera \"" + listItem.name + "\" selected", Toast.LENGTH_SHORT).show();

            ConnectCamera.title = listItem.name;

            IPCamera cameraData = listItem;
            Intent i = new Intent(getApplicationContext(), ConnectCamera.class);
            i.putExtra("selectedCamera", cameraData);
            startActivity(i);
        }
    });
}

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    Log.i(TAG, "Saving state.......");
    savedInstanceState.putParcelableArrayList(LIST_SAVE, listOfCameras);
    savedInstanceState.putSerializable("Camlist", listOfCameras);
    savedInstanceState.putString("test", testString02);

    super.onSaveInstanceState(savedInstanceState);
    System.out.println(savedInstanceState.toString());
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    Log.i(TAG, "RESTORING......");
    System.out.println(savedInstanceState.toString());
    super.onRestoreInstanceState(savedInstanceState);
    listOfCameras = savedInstanceState.getParcelableArrayList(LIST_SAVE);
    testString = savedInstanceState.getString("test");
}
}

class camListAdapter extends ArrayAdapter<IPCamera> {
public camListAdapter(Context context, ArrayList<IPCamera> cams) {
    super(context, 0, cams);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    IPCamera camera = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.select_camera_item, parent, false);
    }
    // Lookup view for data population
    TextView tvName = (TextView) convertView.findViewById(R.id.camName);
    TextView tvHome = (TextView) convertView.findViewById(R.id.camURL);
    // Populate the data into the template view using the data object
    tvName.setText(camera.name);
    tvHome.setText(camera.url);
    // Return the completed view to render on screen
    return convertView;
}
}

正如您所看到的,我有很多印刷语句可以尝试找出出错的原因和原因。无论我如何销毁活动或者如果我移动到另一个活动,Bundle将被保存并且我知道这一点因为当我打印Bundle时活动被破坏它打印出来正确,但是当我返回活动时Bundle永远是空的。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

尝试在该方法的末尾(最后一行)调用super.onSaveInstanceState,并在开头(第一行)调用super.onRestoreInstanceState