[Activity #1] onSaveInstanceState()

Posted by [하늘이]
2010. 12. 2. 17:38 IT/Android[안드로이드]
반응형
void onSaveInstanceState(Bundle outState)
Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) oronRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both).

Activity 내에서 화면이 전환된다던지, 특정 상황에 Activity에서 사용하던 정보를 저장하고 싶은데 사용하면됩니다.
기본 흐름.
1. 호출될때 데이타를 저장합니다.
public class SearchActivity extends Activity {
    private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
    private String mCity = "";  
........
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("mAppWidgetId", mAppWidgetId);
outState.putString("mCity", mCity);
......
}


2. Oncreate에서 데이타를 꺼내면 됩니다.

    public void onCreate(Bundle savedInstanceState) 
    {    
        if(savedInstanceState == null){
........
        }else{
........
mAppWidgetId = savedInstanceState.getInt("mAppWidgetId");
mCity = savedInstanceState.getString("mCity");  
........
        }

간단하죠.



반응형