Activity restart on rotation Android

2018-12-30 23:56发布

In my Android application, when I rotate the device (slide out the keyboard) then my Activity is restarted (onCreate is called). Now, this is probably how it's supposed to be, but I do a lot of initial setting up in the onCreate method, so I need either:

  1. Put all the initial setting up in another function so it's not all lost on device rotation or
  2. Make it so onCreate is not called again and the layout just adjusts or
  3. Limit the app to just portrait so that onCreate is not called.

30条回答
柔情千种
2楼-- · 2018-12-31 00:49

The onCreate method is still called even when you change the orientation of android. So moving all the heavy functionality to this method is not going to help you

查看更多
回忆,回不去的记忆
3楼-- · 2018-12-31 00:49

Note: I post this answer if someone in the future face the same problem as me. For me the following line wasn't enought:

android:configChanges="orientation"

When I rotated the screen, the method `onConfigurationChanged(Configuration newConfig) did't get called.

Solution: I also had to add "screenSize" even if the problem had to do with the orientation. So in the AndroidManifest.xml - file, add this:

android:configChanges="keyboardHidden|orientation|screenSize"

Then implement the method onConfigurationChanged(Configuration newConfig)

查看更多
梦该遗忘
4楼-- · 2018-12-31 00:51

Add this line in manifest : android:configChanges="orientation|screenSize"

查看更多
临风纵饮
5楼-- · 2018-12-31 00:51

Put this below code in your Activity in Android Manifest.

android:configChanges="orientation"

This will not restart your activity when you would change orientation.

查看更多
还给你的自由
6楼-- · 2018-12-31 00:55

what I did...

in the manifest, to the activity section, added:

android:configChanges="keyboardHidden|orientation"

in the code for the activity, implemented:

//used in onCreate() and onConfigurationChanged() to set up the UI elements
public void InitializeUI()
{
    //get views from ID's
    this.textViewHeaderMainMessage = (TextView) this.findViewById(R.id.TextViewHeaderMainMessage);

    //etc... hook up click listeners, whatever you need from the Views
}

//Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    InitializeUI();
}

//this is called when the screen rotates.
// (onCreate is no longer called when screen rotates due to manifest, see: android:configChanges)
@Override
public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.main);

    InitializeUI();
}
查看更多
墨雨无痕
7楼-- · 2018-12-31 00:55

I just discovered this lore:

For keeping the Activity alive through an orientation change, and handling it through onConfigurationChanged, the documentation and the code sample above suggest this in the Manifest file:

android:configChanges="keyboardHidden|orientation"

which has the extra benefit that it always works.

The bonus lore is that omitting the keyboardHidden may seem logical, but it causes failures in the emulator (for Android 2.1 at least): specifying only orientation will make the emulator call both OnCreate and onConfigurationChanged sometimes, and only OnCreate other times.

I haven't seen the failure on a device, but I have heard about the emulator failing for others. So it's worth documenting.

查看更多
登录 后发表回答