I have an app where I save the values chosen from a spinner into a list view. The problem is, when you turn off the device, or remove the app from multitasking, the list view becomes blank when you run the app again. However, when you run the app from multitasking, the values are still there and they don't disappear. My code:
ListActivity.java
package viva.inspection.com.inspectionpicker;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import viva.inspection.com.inspectionpicker.R;
public class ListActivity extends Activity {
private static ArrayList<String> inspections = new ArrayList<String>();
private static ArrayAdapter<String> inspectionAdapter;
String value;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
ListView inspectionList = (ListView) findViewById(R.id.listView);
inspectionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, inspections);
inspectionList.setAdapter(inspectionAdapter);
if(getIntent().getStringExtra("spins") != null) {
value = getIntent().getStringExtra("spins");
}
if(value != null) {
addItems(value);
}
}
public static void addItems(String s) {
inspections.add(s);
inspectionAdapter.notifyDataSetChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch(id) {
case R.id.action_settings:
return true;
case R.id.action_new:
startActivity(new Intent(this, MyActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
EDIT: I saw this question asked earlier that sort of solves my problem. I don't have an arraylist of custom objects, however, it's just an arraylist of strings, so how do I implement the Parcelable interface.
EDIT #2: I have solved my problem using this code:
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putStringArrayList("inspection list", inspections);
}
Then I modified the onCreate method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
ListView inspectionList = (ListView) findViewById(R.id.listView);
inspectionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, inspections);
inspectionList.setAdapter(inspectionAdapter);
if(getIntent().getStringExtra("spins") != null) {
value = getIntent().getStringExtra("spins");
}
if(value != null) {
addItems(value);
}
if(savedInstanceState != null ) {
//Then the application is being reloaded
inspections = savedInstanceState.getStringArrayList("inspection list");
}
}
Thank you to those who helped. I appreciate it.