• <bdo id='xVYpd'></bdo><ul id='xVYpd'></ul>

    <small id='xVYpd'></small><noframes id='xVYpd'>

  • <legend id='xVYpd'><style id='xVYpd'><dir id='xVYpd'><q id='xVYpd'></q></dir></style></legend>
      <tfoot id='xVYpd'></tfoot>

      1. <i id='xVYpd'><tr id='xVYpd'><dt id='xVYpd'><q id='xVYpd'><span id='xVYpd'><b id='xVYpd'><form id='xVYpd'><ins id='xVYpd'></ins><ul id='xVYpd'></ul><sub id='xVYpd'></sub></form><legend id='xVYpd'></legend><bdo id='xVYpd'><pre id='xVYpd'><center id='xVYpd'></center></pre></bdo></b><th id='xVYpd'></th></span></q></dt></tr></i><div id='xVYpd'><tfoot id='xVYpd'></tfoot><dl id='xVYpd'><fieldset id='xVYpd'></fieldset></dl></div>

        旋转Android上的活动重启

        Activity restart on rotation Android(旋转Android上的活动重启)
          • <bdo id='9mX80'></bdo><ul id='9mX80'></ul>

              <legend id='9mX80'><style id='9mX80'><dir id='9mX80'><q id='9mX80'></q></dir></style></legend>
                <tbody id='9mX80'></tbody>

                  <i id='9mX80'><tr id='9mX80'><dt id='9mX80'><q id='9mX80'><span id='9mX80'><b id='9mX80'><form id='9mX80'><ins id='9mX80'></ins><ul id='9mX80'></ul><sub id='9mX80'></sub></form><legend id='9mX80'></legend><bdo id='9mX80'><pre id='9mX80'><center id='9mX80'></center></pre></bdo></b><th id='9mX80'></th></span></q></dt></tr></i><div id='9mX80'><tfoot id='9mX80'></tfoot><dl id='9mX80'><fieldset id='9mX80'></fieldset></dl></div>

                  <small id='9mX80'></small><noframes id='9mX80'>

                  <tfoot id='9mX80'></tfoot>
                1. 本文介绍了旋转Android上的活动重启的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  在我的 Android 应用程序中,当我旋转设备(滑出键盘)时,我的 Activity 会重新启动(调用 onCreate).现在,这可能就是它应该的样子,但是我在 onCreate 方法中做了很多初始设置,所以我需要:

                  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. 将所有初始设置放在另一个函数中,这样它就不会在设备旋转时全部丢失或
                  2. 使 onCreate 不再被调用,布局只是调整或
                  3. 将应用限制为仅纵向,以便不调用 onCreate.
                  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.

                  推荐答案

                  使用应用程序类

                  根据您在初始化中所做的事情,您可以考虑创建一个扩展 Application 的新类,并将您的初始化代码移动到该类中重写的 onCreate 方法中.

                  Depending on what you're doing in your initialization you could consider creating a new class that extends Application and moving your initialization code into an overridden onCreate method within that class.

                  public class MyApplicationClass extends Application {
                    @Override
                    public void onCreate() {
                      super.onCreate();
                      // TODO Put your application initialization code here.
                    }
                  }
                  

                  应用程序类中的onCreate仅在整个应用程序创建时被调用,因此Activity在方向重启或键盘可见性变化时不会触发它.

                  The onCreate in the application class is only called when the entire application is created, so the Activity restarts on orientation or keyboard visibility changes won't trigger it.

                  最好将此类的实例公开为单例,并使用 getter 和 setter 公开您正在初始化的应用程序变量.

                  It's good practice to expose the instance of this class as a singleton and exposing the application variables you're initializing using getters and setters.

                  注意:您需要在清单中指定新应用程序类的名称才能注册和使用它:

                  <application
                      android:name="com.you.yourapp.MyApplicationClass"
                  

                  对配置更改做出反应 [更新:自 API 13 起已弃用;查看推荐的替代方案]

                  作为另一种选择,您可以让应用程序侦听可能导致重启的事件(例如方向和键盘可见性更改)并在您的 Activity 中处理它们.

                  As a further alternative, you can have your application listen for events that would cause a restart – like orientation and keyboard visibility changes – and handle them within your Activity.

                  首先将 android:configChanges 节点添加到 Activity 的清单节点

                  Start by adding the android:configChanges node to your Activity's manifest node

                   <activity android:name=".MyActivity"
                        android:configChanges="orientation|keyboardHidden"
                        android:label="@string/app_name">
                  

                  或对于 Android 3.2(API 级别 13)及更高版本:

                  <activity android:name=".MyActivity"
                        android:configChanges="keyboardHidden|orientation|screenSize"
                        android:label="@string/app_name">
                  

                  然后在 Activity 中重写 onConfigurationChanged 方法并调用 setContentView 以强制在新方向上重新完成 GUI 布局.

                  Then within the Activity override the onConfigurationChanged method and call setContentView to force the GUI layout to be re-done in the new orientation.

                  @Override
                  public void onConfigurationChanged(Configuration newConfig) {
                    super.onConfigurationChanged(newConfig);
                    setContentView(R.layout.myLayout);
                  }
                  

                  这篇关于旋转Android上的活动重启的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How To Create a Rotating Wheel Control?(如何创建转轮控件?)
                  iOS 6 rotations: supportedInterfaceOrientations doesn#180;t work?(iOS 6 旋转:supportedInterfaceOrientations 不起作用?)
                  CABasicAnimation rotate returns to original position(CABasicAnimation 旋转返回原始位置)
                  How to avoid restarting activity when orientation changes on Android(如何在 Android 上的方向更改时避免重新启动活动)
                  UITabBarController Rotation Issues in ios 6(ios 6 中的 UITabBarController 旋转问题)
                  iOS: How to run a function after Device has Rotated (Swift)(iOS:设备旋转后如何运行函数(Swift))
                      <legend id='zgN0f'><style id='zgN0f'><dir id='zgN0f'><q id='zgN0f'></q></dir></style></legend>
                        <tbody id='zgN0f'></tbody>

                          <bdo id='zgN0f'></bdo><ul id='zgN0f'></ul>
                            <i id='zgN0f'><tr id='zgN0f'><dt id='zgN0f'><q id='zgN0f'><span id='zgN0f'><b id='zgN0f'><form id='zgN0f'><ins id='zgN0f'></ins><ul id='zgN0f'></ul><sub id='zgN0f'></sub></form><legend id='zgN0f'></legend><bdo id='zgN0f'><pre id='zgN0f'><center id='zgN0f'></center></pre></bdo></b><th id='zgN0f'></th></span></q></dt></tr></i><div id='zgN0f'><tfoot id='zgN0f'></tfoot><dl id='zgN0f'><fieldset id='zgN0f'></fieldset></dl></div>

                            <tfoot id='zgN0f'></tfoot>

                          • <small id='zgN0f'></small><noframes id='zgN0f'>