Programing

화면 회전 후 TextView 상태 복원?

crosscheck 2020. 8. 16. 19:15
반응형

화면 회전 후 TextView 상태 복원?


내 앱에는 TextViewEditText. 둘 다 데이터가 있습니다. 화면 방향이 바뀌면 EditText잔재 의 데이터가 변경 되지만 TextView데이터는 지워집니다.

누군가가 데이터를 보관하는 방법을 찾는 데 도움을 줄 수 있습니까 TextView?


TextView상태 를 강제 로 저장하려면 freezesText속성을 추가해야 합니다.

<TextView 
     ... 
     android:freezesText="true" />

에 대한 문서에서 freezesText:

설정된 경우 텍스트보기는 현재 커서 위치와 같은 메타 데이터와 함께 고정 된 고드름 내부에 현재 전체 텍스트를 포함합니다. 기본적으로 비활성화되어 있습니다. 텍스트 뷰의 내용이 컨텐트 제공자와 같은 영구적 인 장소에 저장되지 않을 때 유용 할 수 있습니다.


방향 변경에 대한 데이터를 유지하려면 두 가지 방법을 구현해야합니다.

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    // Read values from the "savedInstanceState"-object and put them in your textview
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    // Save the values you need from your textview into "outState"-object
    super.onSaveInstanceState(outState);
}

1) ID가있는 모든보기가 상태를 저장하는 것은 아닙니다. 사용자가 상태를 변경할 수있는 ID가있는 Android 위젯은 소프트 킬시 상태를 저장하는 것처럼 보입니다. 따라서 EditText는 상태를 저장하지만 TextView는 소프트 킬 상태를 저장하지 않습니다.

"AFAIK, Android는 변경 될 것으로 예상되는 항목에 대해서만 상태를 저장합니다. 그렇기 때문에 텍스트를 EditText (사용자가 변경 될 가능성이 있음)에 저장하고 TextView (일반적으로 정적으로 유지됨)에 대한 상태를 저장하지 않을 수도 있습니다. ) "

마크 M

따라서 onSaveInstanceState에 textview의 상태를 저장하도록 선택하고 onCreate에서 textview의 상태를 복원하도록 선택할 수 있습니다.

2) 모범 사례는 선언하더라도 "내부"비보기 인스턴스 상태를 저장하는 것입니다.

android:configChanges= "orientation|keyboardHidden" 

문서에서 :

"그러나 응용 프로그램은 항상 이전 상태를 그대로 유지 한 상태에서 종료하고 다시 시작할 수 있어야합니다. 응용 프로그램을 다시 시작하는 것을 막을 수없는 다른 구성 변경 사항이있을뿐만 아니라 사용자가 수신 메시지를받을 때와 같은 이벤트를 처리하기 위해 전화를 건 다음 애플리케이션으로 돌아갑니다. "

JAL


Android는 이러한 종류의 작업을 처리하지 않습니다. 모든 데이터를 수동으로 저장했습니다.

활동에서 사용하여 값을 저장할 수 있습니다.

@Override
    public Object onRetainNonConfigurationInstance() {
        HashMap<String, Object> savedValues = new HashMap<String, Object>();
        savedValues.put("someKey", someData);           
        return savedValues;
    }

활동의 oncreate 메소드에서 이와 같은 것을 사용하여 저장된 객체를로드합니다.

HashMap < String, Object> savedValues 
     = (HashMap<String, Object>)this.getLastNonConfigurationInstance();

활동에 대한 방향 변경 비활성화를 선택할 수도 있습니다.

<activity android:name=".Activity" android:screenOrientation="portrait" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>
</activity>

AndroidManifest.xml에서 활동을 수정하여 활동 / 활동에 다음을 추가하여 방향 변경 동작을 재정의합니다.

android:configChanges="orientation|keyboardHidden"

다음과 같이 보일 것입니다.

    <activity android:name=".activity.LoginActivity"
              android:configChanges="orientation|keyboardHidden"
              android:label="@string/app_name">
    </activity>

Add these to AndroidManifest.xml
Put your Activity Name in the place of Activity_Name

<activity android:name="Activity_Name"
        android:screenOrientation="sensor"
        ...
        android:configChanges="orientation|keyboardHidden|screenSize">

This will work with value changing TextField also.


just add

android:configChanges="orientation|screenSize"

to your Activity in AndroidManifest.xml.


When configuration changes Android restarts your activity by default. To change this you should override onConfigurationChanged() method. Also, you should add android:configChanges to your manifest file.

You can read more here.


I also used

android:configChanges="orientation"

and it did not work.

But then I found a solution.

Verify if you have the following line correctly in your maniest:

<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="11" />

make sure it does not read something like

<uses-sdk android`:minSdkVersion="11" android:targetSdkVersion="15" />`

I had it the second way in first place but when I corrected it state got preserved.

참고URL : https://stackoverflow.com/questions/5179686/restoring-state-of-textview-after-screen-rotation

반응형