通过以下部分代码,我们可以了解清楚Activity页面在横,竖屏切换时,生命周期的变化:
Java代码
public class AndroidLifecycle extends Activity {
public void onCreate(Bundle savedInstanceState) {
System.out.println("First Activity =======onCreate()========");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
System.out
.println("First Activity =======onSaveInstanceState()========");
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle outState) {
System.out
.println("First Activity =======onRestoreInstanceState()========");
super.onRestoreInstanceState(outState);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
System.out
.println("First Activity =======onConfigurationChanged()========");
super.onConfigurationChanged(newConfig);
}
// Called after onCreate — or after onRestart when the activity had been
// stopped, but is now again being displayed to the user. It will be
// followed by onResume
protected void onStart() {
System.out.println("First Activity =======onStart()========");
super.onStart();
}
// Called after onRestoreInstanceState, onRestart, or onPause, for your
// activity to start interacting with the user
protected void onResume() {
System.out.println("First Activity =======onResume()========");
super.onResume();
}
// Called as part of the activity lifecycle when an activity is going into
// the background, but has not (yet) been killed
protected void onPause() {
System.out.println("First Activity =======onPause()========");
super.onPause();
}
// Called when you are no longer visible to the user. You will next receive
// either onRestart, onDestroy, or nothing, depending on later user
// activity.
protected void onStop() {
System.out.println("First Activity =======onStop()========");
super.onStop();
}
// Perform any final cleanup before an activity is destroyed
protected void onDestroy() {
System.out.println("First Activity =======onDestroy()========");
super.onDestroy();
}
// Called after onStop when the current activity is being re-displayed to
// the user (the user has navigated back to it). It will be followed by
// onStart and then onResume
protected void onRestart() {
System.out.println("First Activity =======onRestart()========");
super.onRestart();
}
}
public class AndroidLifecycle extends Activity {
public void onCreate(Bundle savedInstanceState) {
System.out.println("First Activity =======onCreate()========");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
System.out
.println("First Activity =======onSaveInstanceState()========");
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle outState) {
System.out
.println("First Activity =======onRestoreInstanceState()========");
super.onRestoreInstanceState(outState);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
System.out
.println("First Activity =======onConfigurationChanged()========");
super.onConfigurationChanged(newConfig);
}
// Called after onCreate — or after onRestart when the activity had been
// stopped, but is now again being displayed to the user. It will be
// followed by onResume
protected void onStart() {
System.out.println("First Activity =======onStart()========");
super.onStart();
}
// Called after onRestoreInstanceState, onRestart, or onPause, for your
// activity to start interacting with the user
protected void onResume() {
System.out.println("First Activity =======onResume()========");
super.onResume();
}
// Called as part of the activity lifecycle when an activity is going into
// the background, but has not (yet) been killed
protected void onPause() {
System.out.println("First Activity =======onPause()========");
super.onPause();
}
// Called when you are no longer visible to the user. You will next receive
// either onRestart, onDestroy, or nothing, depending on later user
// activity.
protected void onStop() {
System.out.println("First Activity =======onStop()========");
super.onStop();
}
// Perform any final cleanup before an activity is destroyed
protected void onDestroy() {
System.out.println("First Activity =======onDestroy()========");
super.onDestroy();
}
// Called after onStop when the current activity is being re-displayed to
// the user (the user has navigated back to it). It will be followed by
// onStart and then onResume
protected void onRestart() {
System.out.println("First Activity =======onRestart()========");
super.onRestart();
}
}
AndroidMenifest.xml文件:
Java代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.d" android:versionCode="1" android:versionName="1.0">
<!-- android:configChanges="orientation" -->
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidLifecycle" android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.d" android:versionCode="1" android:versionName="1.0">
<!-- android:configChanges="orientation" -->
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidLifecycle" android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
通过以上代码,我们可以知道:
1.不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次.
2.设置Activity的android:configChanges="orientation"时,切屏还是会重新调用各个生命周期,切横、竖屏时只会执行一次.
3.设置Activity的android:configChanges="orientation|keyboardHidden"时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法.
分享到:
相关推荐
在Android应用开发中,...理解并妥善处理横竖屏切换下的Activity生命周期,是Android开发中的重要技能,它有助于提高应用的稳定性和用户体验。通过合理的设计和编程,开发者可以让应用在任何屏幕方向下都能流畅运行。
综上所述,“LandscapePortraitDemo”项目将展示如何在Android Studio中实现Activity的横竖屏切换,处理WebView的加载,以及对Activity生命周期的精细控制。通过对这些关键点的掌握,开发者能够构建出适应各种屏幕...
本篇文章将详细探讨Android横竖屏切换时Activity的生命周期变化。 首先,一个新建的Activity在启动时会依次经历`onCreate()`、`onStart()`和`onResume()`方法,这表示Activity已经创建并可见于用户。当用户按`Ctrl+...
android activity 执行屏幕横竖屏切换的时候activity声明周期执行的竖屏的时候点击横屏 09-04 15:54:27.675: I/yxd(6708): 应用程序执行了============onPause===方法===>> 09-04 15:54:27.685: I/yxd(6708): 应用...
关于Activity生命周期和横竖屏切换时,生命周期的执行过程,网上有很多文章。但是都写的很模糊,并且不完善。一般的我们去切换屏幕方向都是不希望Activity被重新创建,这时就需要对一些属性进行设置,或者使用代码...
总之,"fragment横竖屏切换demo"项目提供了关于如何在Android中使用Fragment处理屏幕方向变化的实际示例,涉及到了Fragment的生命周期管理、状态保存、布局调整以及与Activity的通信等多个关键知识点。在实际开发中...
当Activity因横竖屏切换而重建时,其生命周期方法会被调用。为了确保数据不丢失,我们需要在`onSaveInstanceState()`中保存关键数据,并在`onCreate()`或`onRestoreInstanceState()`中恢复它们。 四、适配不同屏幕...
在Android开发中,横竖屏切换是一个常见的需求,尤其对于那些需要根据屏幕方向提供不同界面布局的应用来说。本文将深入探讨Android系统中的横竖屏切换机制,并通过"OrientationSwitchDemo"这个示例代码来帮助理解。 ...
### Activity的生命周期与横竖屏切换时的状态变化 在Android应用开发中,`Activity`作为四大组件之一,是用户交互的主要界面。理解`Activity`的生命周期对于开发流畅且高效的用户体验至关重要。本文将深入探讨`...
为了避免这种情况,可以在AndroidManifest.xml中为相应Activity设置`android:configChanges="orientation|screenSize"`,然后在Activity中覆盖onConfigurationChanged()方法,处理横竖屏切换。 ```xml <activity ...
当Android手机进行横竖屏切换时,Activity的生命周期默认会发生变化,这可能导致数据丢失、用户体验中断,特别是在需要保持状态的游戏或复杂应用中尤为明显。本篇将详细探讨如何避免这种生命周期变化,以确保在横竖...
接下来,我们来看横竖屏切换对Activity生命周期的影响: 1. **不指定`android:configChanges`**:默认情况下,当设备横竖屏切换时,系统会销毁当前Activity并重新创建,这会导致`onCreate()`、`onStart()`和`...
理解Fragment的生命周期、如何在Activity中添加和管理Fragment,以及如何处理横竖屏切换时的状态保存,是每个Android开发者必须掌握的基本技能。在实际项目中,还需要注意性能优化和用户体验,确保应用在各种场景下...
默认情况下,这些变更会导致Activity生命周期中的`onDestroy()`和`onCreate()`方法被调用。 2. **重用Activity状态**:为了保留Activity的状态,开发者可以使用`onSaveInstanceState()`方法来保存关键数据。当...
综上所述,实现SurfaceView支持横竖屏切换、视频适配屏幕以及home退出后继续播放的功能,需要对Android的生命周期管理、布局设计、多媒体播放以及服务有深入的理解。开发者需要处理好SurfaceView的生命周期,确保...
1. **Activity生命周期与横竖屏切换**: 当设备屏幕方向发生变化时,Android系统会默认销毁当前Activity并重建,以加载对应方向的布局资源。在这个过程中,Activity的生命周期方法会被调用,包括`...
在Android系统中,横竖屏切换默认会导致Activity重建,这可能导致UI布局的改变和应用数据的丢失。因此,开发者需要对此进行妥善处理,确保用户体验的连续性。以下是横竖屏切换处理的关键知识点: 1. **配置变更...