문제

I have been working on an andoid app since a few days. I have two layouts in my MainActivity, first one shows SearchBox and second shows the Results. When the app shows the results and the app orientation is changed, the app get back to the first screen. I may not be clear with my engish but see these simple steps :

 Open App -> 1st screen(MainActivity) -> 2nd Screen(MainActivity) -> Now If I rotate the screen It goes back to 1st screen.

I searched on internet about it, but I was unable to find anything on it. Any help would be Appreciated to solve this.

도움이 되었습니까?

해결책

You should use onSaveInstanceState to save your state, and then recreate it in onCreate.

Read following answer

Restore values on screen rotation

다른 팁

On screen orientation change Activity getting restarted so if you are not saving state of it then it can destroy due to error like null pointer exception and sometime android not shows any log of forceclose for saving state of your activity take a look at this : Configuration Changes

When you change the orientation of the device the app is being destroyed and then restarted. For you app to remember where the user was, plus other things, you need to implement code in your onCreate to read the saved state and also you need to save the state before the app is closed, this can be done in a number of places and where depends on your needs but could be onPause(), onDestroy or

@Override
protected void onSaveInstanceState( Bundle outState )
{
            // save your sate here
    super.onSaveInstanceState( outState );
}

protected void onCreate( Bundle savedInstanceState )
{
    super.onCreate( savedInstanceState );

    setContentView( R.layout.layout_main );

    // if there is a saved state restore it
    if( savedInstanceState != null )
    {
    }
    else
    {

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