어떤 Android 이벤트가 활동이 파괴 될 때까지 한 번 호출됩니까?

StackOverflow https://stackoverflow.com/questions/9027507

  •  14-11-2019
  •  | 
  •  

문제

나는 한 번의 대답을 찾고 있습니다 (그러나 나는 잘못된 질문을 할 수 있음)

질문 - 모든 이벤트가 활동이 파괴 될 때까지 한 번만 한 번 호출됩니까?

사용자가 oncreate와 onstart를 가로 끌기 위해 전화를 회전하면 onstart를 다시로드하면됩니다.

나는 그 행동을 할 수있는 이벤트를 찾고 있습니다 (활동이 사망 할 때까지)

미리 감사드립니다

도움이 되었습니까?

해결책

If it is specific to the Activity just check your savedInstanceState parameter in the onCreate event. If it is null, run your code, if not, your code has already been run.

Example:

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

    if(savedInstanceState == null) {
        // Run your code
    }        
}

savedInstanceState will always be null when onCreate is run for the first time, and it will be populated thereafter.

다른 팁

You don't really specify what you're trying to do with it, so I can't guarantee this is appropriate for your use, but Application.onCreate is only called once.

If you want to eliminate the recreation of your activity on an orientationchange you can listen for configchanges in the manifest.

    <activity
            android:name=".MyActivity"
            android:configChanges="orientation" >
    </activity>

And then you can override onConfigurationChanged like so:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged( newConfig );

    LinearLayout main = (LinearLayout) findViewById( R.id.mainLayout );
    main.requestLayout();
}

to recreate the layout so that it matches the new orientation, without recreating the entire activity.

Check http://developer.android.com/guide/topics/resources/runtime-changes.html to handle configuration changes and to maintain your huge data between them...if all you need to maintain between the configuration change is just the settings,you can use the onSavedInstanceState() and onRestoreInstanceState() callbacks and the given bundles.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top