2016-04-15 107 views
0

我知道這個問題隨處可見,而且我試過尋找這樣的答案:onSaveInstanceState not working,因爲它與我的類似,但是我找不到解決方案。保存和恢復活動的狀態 - Bundle始終爲空?

這是我的代碼:

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

正如你可以看到我有很多的print語句,試圖找出什麼錯誤,爲什麼。無論我如何銷燬活動,或者如果我移動到其他活動,Bundle都會得到保存,我知道這一點,因爲當打印Bundle時,活動被銷燬,它會正確打印,但是當我返回活動時Bundle始終爲空。任何幫助?

+1

捆綁的onSaveInstanceState中和onRestoreInstanceState使用不保存數據持久......它更因爲(極大簡化)在活動backstack或方向更改之間存儲數據... – Selvin

+0

@Selvin我將如何去保存數據持久性?因爲這是我想要達到的目標 – Mattzo26

回答

0

嘗試致電super.onSaveInstanceState在該方法(最後一行)的結束,super.onRestoreInstanceState開頭(第一行)

+1

當超級方法被調用時onCreate仍然會做正確的事情並不重要 –

+0

我試過這個,它只是導致應用程序崩潰 – Mattzo26