App shows main screen after screen rotation

南笙酒味 提交于 2020-01-05 04:57:39

问题


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.


回答1:


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

Read following answer

Restore values on screen rotation




回答2:


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




回答3:


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
    {

    }
}


来源:https://stackoverflow.com/questions/18533693/app-shows-main-screen-after-screen-rotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!