android2018. 7. 16. 11:14




Createing vies from scrach의 경우, Orientation 변경 시 view의 state 값이 저장되지 않는다.

Combining existing view의 경우는 view의 ID가 있으면 state 저장하도록 구현되어있음.


Tip)

An EditText by default will save its state, as long as an ID is provided in the layout.

That last part is important: without an ID no view will be able to save and restore state.



View clas의 BaseSavedState 를 확장하여 저장하고 싶은 값을 추가해보자.

알아야할 두가지 포인트는 constructor에서 restoring 시점에 Parcelable 객체를 받아온다는 것.

그리고 writeToParcel 부분에서 원하는 데이터를 저장한다는 것이다.

결론적으로 writeToParcel를 override하고, 2nd Constructor에서 값을 read하면 된다.




SavedState.java

import android.os.Parcel;
import android.os.Parcelable;
import android.view.View;

public class SavedState extends View.BaseSavedState {
    int value;

    public SavedState(Parcelable superState) {
        super(superState);
    }
    private SavedState(Parcel in ){
        super(in);
        value = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        super.writeToParcel(out, flags);
        out.writeInt(value);
    }

    public static final Parcelable.Creator CREATOR=
        new Parcelable.Creator(){
            public SavedState createFromParcel (Parcel in ){
                return new SavedState(in);

            }

            @Override
            public SavedState[] newArray(int i) {
                return new SavedState[0];
            }
        };
}


ValueBar.java

    @Override
    protected Parcelable onSaveInstanceState() {
        Parcelable superState = super.onSaveInstanceState();
        SavedState ss = new SavedState(superState);
        ss.value = currentValue;
        Log.d("hello","onSaveInstanceState -> currentValue ="+currentValue);
        return ss;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        SavedState ss = (SavedState) state;
        super.onRestoreInstanceState(ss.getSuperState());
        currentValue=ss.value;
        Log.d("hello","onRestoreInstanceState -> currentValue ="+currentValue);
    }


//값을 위 두개의 callback의 사용 여부를 아래의 함수로 명시적으로 표시 가능하다.

//false 선택시, 호출되지 않으며 default는 true로 설정되어있다.

setSaveEnabled(true);





테스트를 위해 orientation을 변경하는 방법은 android:configChanges를 아래와 같이 설정 후, 

아래의 코드를 사용했다.


Manifest의 activity에 아래와 같이 


<activity android:name=".MainActivity"

android:configChanges="orientation|keyboardHidden"

>



if ( getRequestedOrientation() ==ActivityInfo.SCREEN_ORIENTATION_PORTRAIT )
	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
else
	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


출처 : 

https://www.intertech.com/Blog/android-custom-view-tutorial-part-3-saving-state/

https://developer.android.com/reference/android/app/Activity

https://stackoverflow.com/questions/15632360/android-manual-screen-orientation-without-restarting-the-activity

'android' 카테고리의 다른 글

Custom Intent로 app 실행하기  (0) 2018.07.16
tutorialspoint  (0) 2018.07.16
custom view tutorial 따라하기 2  (0) 2018.07.13
custom view tutorial 따라하기 1  (0) 2018.07.12
Simple voice search test2  (0) 2018.07.11
Posted by easy16