onSaveInstanceState () 및 onRestoreInstanceState ()
내가 저장하고의 상태를 복원하기 위해 노력하고있어 Activity
방법을 사용을 onSaveInstanceState()
하고 onRestoreInstanceState()
.
문제는 onRestoreInstanceState()
메소드에 절대 들어 가지 않는다는 것입니다. 왜 이것이 나에게 설명 할 수 있습니까?
일반적으로에서 상태를 복원합니다 onCreate()
. 복원도 가능 onRestoreInstanceState()
하지만 일반적이지 않습니다. ( onRestoreInstanceState()
이후 onStart()
에 호출 되는 반면 onCreate()
에 , 전에 호출 됨) onStart()
.
put 메소드를 사용하여 다음에 값을 저장하십시오 onSaveInstanceState()
.
protected void onSaveInstanceState(Bundle icicle) {
super.onSaveInstanceState(icicle);
icicle.putLong("param", value);
}
그리고 다음 값을 복원하십시오 onCreate()
.
public void onCreate(Bundle icicle) {
if (icicle != null){
value = icicle.getLong("param");
}
}
super.onSaveInstanceState (icicle);를 호출하여 자동으로 저장되므로보기 상태를 저장할 필요가 없습니다 .
onRestoreInstanceState()
라고 다시 때만 이 후 활동을 살해 OS에 의해. 이러한 상황은 다음과 같은 경우에 발생합니다.
- 장치 방향 변경 (활동이 파괴되고 재 생성됨)
- 당신 앞에 또 다른 활동이 있으며 어떤 시점에서 OS는 메모리를 확보하기 위해 활동을 종료합니다 (예 :). 다음에 활동을 시작할 때 전화가 걸립니다
onRestoreInstanceState()
.
대조적으로 : 활동 중이고 Back
장치의 버튼을 누르면 활동이 완료되고 (즉, 데스크탑 응용 프로그램을 종료하는 것으로 생각) 다음에 응용 프로그램을 시작할 때 "새로"시작됩니다. 당신이 때렸을 때 의도적으로 종료했기 때문에 저장된 상태 Back
.
다른 혼란의 원인은 앱 onSaveInstanceState()
이 다른 앱에 대한 포커스를 잃을 때 호출되지만 앱 으로 다시 탐색하면 onRestoreInstanceState()
호출되지 않을 수 있다는 것입니다. 이것은 원래의 질문에 설명 된 경우입니다. 즉, 다른 활동이 시작된 기간 동안 활동이 종료되지 않은 경우 onRestoreInstanceState()
활동이 거의 "활성"이기 때문에 호출되지 않습니다.
에 대한 문서에 명시된 바와 같이 onRestoreInstanceState()
:
대부분의 구현은 단순히 onCreate (Bundle)를 사용하여 상태를 복원하지만 모든 초기화가 완료된 후 여기에서 수행하거나 하위 클래스가 기본 구현을 사용할지 여부를 결정하도록하는 것이 편리한 경우가 있습니다. 이 메소드의 기본 구현은 onSaveInstanceState (Bundle)에 의해 이전에 정지 된 모든 뷰 상태의 복원을 수행합니다.
내가 읽은대로 : onRestoreInstanceState()
서브 클래스를 작성하지 않고 Activity
누군가가 서브 클래스를 서브 클래스 화 할 것으로 예상 되지 않는 한 재정의 할 이유가 없습니다 .
저장 한 상태 onSaveInstanceState()
는 나중에 onCreate()
메소드 호출시 사용 가능 합니다. 따라서 onCreate
(및 해당 Bundle
매개 변수)를 사용 하여 활동 상태를 복원하십시오.
임시 해결책으로 활동 A를 시작하는 데 사용하려는 의도로 유지하려는 데이터가있는 번들을 저장할 수 있습니다.
Intent intent = new Intent(this, ActivityA.class);
intent.putExtra("bundle", theBundledData);
startActivity(intent);
활동 A는 이것을 활동 B로 다시 전달해야합니다. 활동 B의 onCreate 메소드에서 의도를 검색합니다.
Intent intent = getIntent();
Bundle intentBundle;
if (intent != null)
intentBundle = intent.getBundleExtra("bundle");
// Do something with the data.
또 다른 아이디어는 활동 상태를 저장하기 위해 저장소 클래스를 작성하고 각 활동이 해당 클래스를 참조하도록하는 것입니다 (단일 구조를 사용할 수 있음). 그러나 그렇게하는 것이 가치가있는 것보다 더 어려울 수 있습니다.
중요한 것은 당신이 저장하지 않는 경우이다 onSaveInstanceState()
후 onRestoreInstanceState()
호출되지 않습니다. 이것의 주된 차이점이다 restoreInstanceState()
하고 onCreate()
. 실제로 무언가를 보관하십시오. 아마도 이것이 당신의 문제입니다.
다른 활동이 포 그라운드로 올 때 onSaveInstanceState가 항상 호출된다는 것을 알았습니다. 그리고 멈춰 있습니다.
그러나 onCreate 및 onStart도 호출 된 경우에만 onRestoreInstanceState가 호출되었습니다. 그리고 onCreate 및 onStart가 항상 호출 된 것은 아닙니다.
따라서 Android가 활동이 백그라운드로 이동하더라도 상태 정보를 항상 삭제하지는 않는 것 같습니다. 그러나 수명주기 메소드를 호출하여 상태를 안전하게 저장합니다. 따라서 상태가 삭제되지 않으면 Android는 수명주기 메소드를 호출하여 필요하지 않은 상태를 복원하지 않습니다.
그림 2 는이를 설명합니다.
이 스레드는 꽤 오래되었다고 생각합니다. onSaveInstanceState()
전화를 거는 경우라고도 하는 다른 사례를 언급 합니다 Activity.moveTaskToBack(boolean nonRootActivity)
.
당신이 활동의 방향 변경을 처리하는 경우 android:configChanges="orientation|screenSize"
와 onConfigurationChanged(Configuration newConfig)
, onRestoreInstanceState()
호출되지 않습니다.
onRestoreInstanceState가 항상 onSaveInstanceState 다음에 호출 될 필요는 없습니다.
Note that : onRestoreInstanceState will always be called, when activity is rotated (when orientation is not handled) or open your activity and then open other apps so that your activity instance is cleared from memory by OS.
In my case, onRestoreInstanceState
was called when the activity was reconstructed after changing the device orientation. onCreate(Bundle)
was called first, but the bundle didn't have the key/values I set with onSaveInstanceState(Bundle)
.
Right after, onRestoreInstanceState(Bundle)
was called with a bundle that had the correct key/values.
I just ran into this and was noticing that the documentation had my answer:
"This function will never be called with a null state."
In my case, I was wondering why the onRestoreInstanceState wasn't being called on initial instantiation. This also means that if you don't store anything, it'll not be called when you go to reconstruct your view.
I can do like that (sorry it's c# not java but it's not a problem...) :
private int iValue = 1234567890;
function void MyTest()
{
Intent oIntent = new Intent (this, typeof(Camera2Activity));
Bundle oBundle = new Bundle();
oBundle.PutInt("MYVALUE", iValue); //=> 1234567890
oIntent.PutExtras (oBundle);
iRequestCode = 1111;
StartActivityForResult (oIntent, 1111);
}
AND IN YOUR ACTIVITY FOR RESULT
private int iValue = 0;
protected override void OnCreate(Bundle bundle)
{
Bundle oBundle = Intent.Extras;
if (oBundle != null)
{
iValue = oBundle.GetInt("MYVALUE", 0);
//=>1234567890
}
}
private void FinishActivity(bool bResult)
{
Intent oIntent = new Intent();
Bundle oBundle = new Bundle();
oBundle.PutInt("MYVALUE", iValue);//=>1234567890
oIntent.PutExtras(oBundle);
if (bResult)
{
SetResult (Result.Ok, oIntent);
}
else
SetResult(Result.Canceled, oIntent);
GC.Collect();
Finish();
}
FINALLY
protected override void OnActivityResult(int iRequestCode, Android.App.Result oResultCode, Intent oIntent)
{
base.OnActivityResult (iRequestCode, oResultCode, oIntent);
iValue = oIntent.Extras.GetInt("MYVALUE", -1); //=> 1234567890
}
참고URL : https://stackoverflow.com/questions/4096169/onsaveinstancestate-and-onrestoreinstancestate
'IT' 카테고리의 다른 글
JavaScript-현재 날짜에서 첫 번째 요일 가져 오기 (0) | 2020.06.25 |
---|---|
해시 할 Rails 객체 (0) | 2020.06.25 |
정규식을 사용하여 bash에서 검색 및 바꾸기 (0) | 2020.06.25 |
Android : 활동이 실행 중인지 어떻게 확인합니까? (0) | 2020.06.25 |
JQuery.trigger ( 'click')를 얻는 방법; (0) | 2020.06.25 |