2013-04-28 24 views
5

ArrayList được định nghĩa ở cấp lớp. đây là những phương pháp savedInstance tôi:lưu ArrayList trong gói đã lưuInstanceState

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putStringArrayList("savedList", list); 
} 


@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 
    list=savedInstanceState.getStringArrayList("savedList"); 
} 

nhưng vẫn còn, khi tôi thay đổi hướng ArrayList là trống

+1

http://stackoverflow.com/a/4967491/1001401 Hãy thử để có được ArrayList của bạn trong onCreate (Bundle savedInstanceState) từ savedInstanceState – nfirex

Trả lời

2

Khi bạn sử dụng onRestoreInstanceState() để khôi phục lại trạng thái, gọi nó sau onStart() vì vậy bạn cập nhật danh sách của bạn với các trạng thái lưu trữ sau khi bạn xác định bộ điều hợp của mình. Lựa chọn tốt nhất của bạn là khôi phục danh sách theo số onCreate() giống như cách bạn thực hiện trên onRestoreInstanceState(). Bạn cũng có thể xác định lại bộ điều hợp hoặc gọi notifyDataSetChanged().

3
TextView textviewname; 
String textname; 

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

     //declare the ui like this 
    textviewname = (TextView) findViewById(R.id.textviewid); 
     if(savedInstanceState==null){ 
     //the first time the activity was created 
     //do the normal flow of the code 
     //Example:getting the intent data for(savedList)or processing it 
     Intent i = getIntent(); 
     textname = i.getStringExtra("savetext"); 
     //or textname="name_data"; 
     }else{ 
     //this is called when orientation change occur 
     //get the savedata 
     list=savedInstanceState.getStringArrayList("savedList"); 
     textname=savedInstanceState.getString("savetext"); 
     } 

     textviewname.setText(textname); 

} 
@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    //saving data when orientation change trigger 
    //saving data before ondestroy happens 
//onSaveInstanceState is called before going to another activity or orientation change 
    outState.putStringArrayList("savedList", list); 
    outState.putString("savetext", textname); 
    //all imnportant data saved and can be restored 
} 


@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 
    //this is only called after ondestroy-> oncreate occur 
    list=savedInstanceState.getStringArrayList("savedList"); 
    textname=savedInstanceState.getString("savetext"); 
    //oncreate->onSaveInstanceState->ondestroy->oncreate->onRestoreInstanceState 
} 
0

cố gắng sử dụng

list.addAll(savedInstanceState.getStringArrayList("savedList")); 

thay

list=savedInstanceState.getStringArrayList("savedList"); 
Các vấn đề liên quan