3

I'm working on a android application that adds every view programmaticaly. When the user turns the screen, I just want to values that are filled in to be displayed again.
Is there an easy way to let Android do this automaticaly?

My application is completely dynamic so it doens't have a predetermined layout, which makes it a lot harder.

So, how can I easily save the state of my screen?

4

2 回答 2

6

每次方向变化,android都会创建新视图并销毁旧视图。您可以在方向更改时保存数据并在创建新视图时重新初始化

使用onConfigurationChanged活动方法检测方向变化

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

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
  }

不要忘记像这样在您的 AndroidManifest.XML 中编辑适当的元素以包含android:configChanges

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:label="@string/app_name">
于 2012-07-18T09:58:00.633 回答
0

有关 Hein 正确答案的详细解释,请参阅我之前的帖子:

https://stackoverflow.com/a/57239493/5696328

于 2019-07-28T09:30:38.673 回答