0

I know this question is asked everywhere, and I have tried looking at answers such as this one: onSaveInstanceState not working as it is similar to mine however I can't find a solution.

This is my code:

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;
}
}

As you can see I have a lot of print statements to try and work out what's going wrong and why. No matter how I destroy the activity or if I move onto another activity, the Bundle will get saved and I know this because when I print the Bundle as the activity is destroyed it prints out correctly, but then when I return to the activity the Bundle is always null. Any help?

Community
  • 1
  • 1
Mattzo26
  • 1
  • 3
  • 1
    Bundle used in onSaveInstanceState and onRestoreInstanceState is not for saving the data persistent ... it is more for (with great simplification) storing data between activities backstack or orientation change ... – Selvin Apr 15 '16 at 13:00
  • @Selvin how would I go about saving the data persistent? As this is what I would like to achieve – Mattzo26 Apr 15 '16 at 15:50

1 Answers1

0

try to call super.onSaveInstanceState at the end of that method (last line), and super.onRestoreInstanceState at the beginning (first line)

Matias Elorriaga
  • 8,880
  • 5
  • 40
  • 58