当屏幕旋转时,我只需要不做任何改变。我的应用程序显示一个随机图像时,它第一次加载和旋转的设备应 没有选择另一个随机图像。 我如何(简单地)让这种行为停止?
把这个加到你的 AndroidManifest.xml上
AndroidManifest.xml
<activity android:screenOrientation="landscape">
我的意思是,有一个活动标签,添加这作为另一个参数。如果您需要纵向定位,请将横向更改为纵向。希望这个能帮上忙。
将图像细节保存在 onPause()或 onStop()中,并在 onCreate(Bundle savedInstanceState)中使用它来恢复图像。
onPause()
onStop()
onCreate(Bundle savedInstanceState)
编辑:
更多关于实际过程的信息在这里详细介绍 http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle,因为它与之前的 Android 版本不同。
根据我的经验,恰当地处理方向的变化,而不是试图硬塞进一个非默认行为,实际上会更好。
您应该保存当前在 onSaveInstanceState()中显示的映像,并在应用程序再次通过 onCreate()运行时正确地还原它。
onSaveInstanceState()
onCreate()
通常有三种方法可以做到这一点:
正如一些答案所示,您可以区分第一次创建活动和从 savedInstanceState恢复活动的情况。这是通过覆盖 onSaveInstanceState并检查 onCreate的参数来完成的。
savedInstanceState
onSaveInstanceState
onCreate
您可以通过在清单中向 <activity>添加 android:screenOrientation="portrait"(或 "landscape")来锁定一个方向的活动。
<activity>
android:screenOrientation="portrait"
"landscape"
您可以通过在 <activity>标记中指定 android:configChanges="orientation|screenSize"来告诉系统您想自己处理屏幕更改。通过这种方式,活动将不会被重新创建,而是会收到一个回调(您可以忽略它,因为它对您没有用处)。
android:configChanges="orientation|screenSize"
就我个人而言,我会选择(3)。当然,如果锁定应用程序的一个方向是罚款与你,你也可以去(2)。
Xion 的答案很接近,但是 # 3(android:configChanes="orientation")不会起作用,除非应用程序的 API 级别为12或更低。
android:configChanes="orientation"
在 API 级别13或更高的情况下,当方向改变时屏幕大小会发生变化,因此这仍然会导致活动被销毁,并在方向改变时启动。
简单地添加“ screen Size”属性,就像我下面所做的那样:
<activity android:name=".YourActivityName" android:configChanges="orientation|screenSize"> </activity>
现在,当您改变方向(和屏幕大小变化)时,活动保持其状态,并调用 onConfigurationChanged()。这将保持屏幕上的任何东西(即: 网页在一个网络视图) ,当方向发生变化。
onConfigurationChanged()
从这个网站了解到: Http://developer.android.com/guide/topics/manifest/activity-element.html
此外,这显然是一个不好的做法,所以请阅读以下关于处理运行时更改的链接:
Http://developer.android.com/guide/topics/resources/runtime-changes.html
使用: android:configChanges="keyboardHidden|orientation"
android:configChanges="keyboardHidden|orientation"
你只需要进入 AndroidManifest.xml,在你的活动标签里面,你必须输入这行代码,就像上面有人说的:
所以,你会有这样的东西:
<activity android:name="ActivityMenu" android:configChanges="orientation|screenSize"> </activity>
希望能成功!
将此代码添加到包含 WebView的活动中的 onCreate、方法之后
WebView
@Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); } @Override protected void onRestoreInstanceState(Bundle state) { super.onRestoreInstanceState(state); }
<activity android:name="com.example.abc" android:configChanges="orientation|screenSize"></activity>
只需在清单文件的活动选项卡中添加 android:configChanges="orientation|screenSize"。
因此,当方向改变时,Activity 不会重新启动。
正如 Pacerier 所说,
此解决方案是目前为止最好的工作方案
<activity android:configChanges="keyboardHidden|orientation|screenSize" android:name="your activity name" android:label="@string/app_name" android:screenOrientation="landscape"> </activity
并在活动类中添加以下代码
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { //your code } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { //your code } }
为清单中的 所有的应用程序活动标记添加 android:configChanges="keyboardHidden|orientation|screenSize"。
android:configChanges="keyboardHidden|orientation|screenSize"
在清单文件中为每个活动添加以下内容。这将有所帮助
android:configChanges = "orientation|keyboard|keyboardHidden|screenLayout|screenSize"
以上所有的答案对我都不起作用。所以,我已经通过提到 label和 screenOrientation像下面这样固定。现在一切都好了
label
screenOrientation
<activity android:name=".activity.VideoWebViewActivity" android:label="@string/app_name" android:configChanges="orientation|screenSize"/>
Http://animeshrivastava.blogspot.in/2017/08/activity-lifecycle-oncreate-beating_3.html
@Override protected void onSaveInstanceState(Bundle b) { super.onSaveInstanceState(b); String str="Screen Change="+String.valueOf(screenChange)+"...."; Toast.makeText(ctx,str+"You are changing orientation...",Toast.LENGTH_SHORT).show(); screenChange=true; }
我不知道是否是最好的解决方案,但我在这里描述它:
首先,你需要证书与您的类应用程序的应用程序是在您的清单这一点:
<application android:name=".App" ...
第二,在我的课程应用程序我喜欢这样:
public class App extends Application { public static boolean isOrientationChanged = false; @Override public void onCreate() { super.onCreate(); } @Override public void onConfigurationChanged(@NotNull Configuration newConfig) { super.onConfigurationChanged(newConfig); if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE || newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { isOrientationChanged = true; } } }
第三,您需要将一个标志设置为“方向更改”,在我的例子中,我总是在调用应用程序导航中的前一个活动时设置该标志,因此在创建后一个活动时只调用一次。
isOrientationChanged = false;
所以每次我改变屏幕的方向,我设置它每次它改变这个设置,它检查是否有方向的改变,如果有,它根据标志的值来验证。
基本上,每当我提出一个异步改造请求时,我就必须使用它,他称之为每一个改变方向的时刻,不断地使应用程序崩溃:
if (!isOrientationChanged) { presenter.retrieveAddress(this, idClient, TYPE_ADDRESS); }
我不知道这是否是最优雅、最漂亮的解决方案,但至少在这里它是实用的:)
阻止重建活动 通过在 AndroidManifest.xml 中的 Activity 中设置 android: configChanges 标志来处理方向更改的最常见解决方案。使用这个属性,您的 Activity 将不会被重新创建,所有的视图和数据在方向更改后仍然存在。
<activity android:name="com.example.test.activity.MainActivity" android:configChanges="orientation|screenSize|keyboardHidden"/>
这是我的工作