Android:关于onConfigurationChanged()的介绍(转)

本文深入探讨了Android开发中,如何正确配置AndroidManifest.xml文件中的Activity属性以提高应用程序效率。特别是针对广告SDK,详细解释了使用android:configChanges=orientation|keyboard|keyboardHidden的意义,并通过一个示例程序演示了其在屏幕方向变化时的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载:http://www.cnblogs.com/bluestorm/p/3622444.html

从事Android开发,免不了会在应用里嵌入一些广告SDK,在嵌入了众多SDK后,发现几乎每个要求在AndroidManifest.xml申明Activity的广告SDK都会要求加上注明这么一句属性:

android:configChanges="orientation|keyboard|keyboardHidden"

 通过查阅Android API可以得知android:onConfigurationChanged实际对应的是Activity里的 onConfigurationChanged()方法。在AndroidManifest.xml中添加上诉代码的含义是表示在改变屏幕方向、弹出软件 盘和隐藏软键盘时,不再去执行onCreate()方法,而是直接执行onConfigurationChanged()。如果不申明此段代码,按照 Activity的生命周期,都会去执行一次onCreate()方法,而onCreate()方法通常会在显示之前做一些初始化工作。所以如果改变屏幕 方向这样的操作都去执行onCreate()方法,就有可能造成重复的初始化,降低程序效率是必然的了,而且更有可能因为重复的初始化而导致数据的丢失。 这是需要千万避免的。

  为了明白这个问题,特意写了一个Demo用于观察执行结果。  

 1 public class ConsoleActivity extends Activity {
 2     private String str = "0";
 3 
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         //模拟数据初始化
 7         str = "1";
 8         Log.e("FHT", "onCreate:" + str);
 9     }
10     
11     
12 
13     @Override
14     protected void onStart() {
15         super.onStart();
16         //模拟显示之后,数据发生改变
17         str = (new Date()).getTime() + "";
18         Log.e("FHT", "onStart:" + str);
19     }
20 
21 
22 
23     @Override
24     public void onConfigurationChanged(Configuration newConfig) {
25         super.onConfigurationChanged(newConfig);
26         Log.e("FHT", "onConfigurationChanged:" + str);
27     }
28 }

运行结果如下:

从上图可以看出,当屏幕方向发生了三次翻转,三次翻转都没有重新进入onCreate()方法,所以str的值得以延续,如果去除 AndroidManifest.xml中关于onConfigurationChanged的相关代码,程序的执行顺序将发生变化,每次屏幕方向的变化 都将引起str值的重置。这是大多数开发过程中所不希望看到的。

  另外需要注意的是onConfigurationChanged()方法中 的:super.onConfigurationChanged(newConfig);一定不能省去,否则将引 发:android.app.SuperNotCalledException 异常。

 

转载于:https://www.cnblogs.com/smilefortoday/p/4016139.html

我要处理一个大显示下的文本长度适配:不选择文件的情况下,在中文语言下显示正常,切换到文本较长的语言并且字体设置超大,显示设置较大后,文本会把按钮挤到屏幕边缘,怎么处理<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/dialog_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="@dimen/dimen_84dp" android:orientation="vertical"> <com.google.android.material.appbar.COUIDividerAppBarLayout android:id="@+id/appbar" style="@style/CommonAppBarStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/color_transparent" android:clickable="true" android:focusable="true" android:paddingLeft="@dimen/dimen_0dp" android:paddingRight="@dimen/dimen_0dp" app:elevation="@dimen/toolbar_elevation"> <com.coui.appcompat.toolbar.COUIToolbar android:id="@+id/toolbar" style="@style/COUIToolBarInAppBarLayoutStyle" android:layout_width="match_parent" android:background="@null" app:supportTitleTextAppearance="@style/textAppearanceSecondTitle" app:titleCenter="false" /> <com.coui.appcompat.tablayout.COUITabLayout android:id="@+id/tab_layout" android:background="@color/color_transparent" style="@style/COUISmallTabLayoutStyle" /> </com.google.android.material.appbar.COUIDividerAppBarLayout> <RelativeLayout android:id="@+id/content_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <com.filemanager.common.view.ViewPagerWrapperForPC android:id="@+id/view_pager_wrapper" android:layout_width="match_parent" android:layout_height="match_parent"> <com.filemanager.common.view.viewpager.RTLViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/appbar" android:clipChildren="false" android:clipToPadding="false" android:orientation="horizontal" /> </com.filemanager.common.view.ViewPagerWrapperForPC> </RelativeLayout> </LinearLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="?attr/couiColorBackgroundElevatedWithCard" android:minHeight="@dimen/operation_btn_background_height"> <View android:id="@+id/button_divider" android:layout_width="match_parent" android:layout_height="@dimen/divider_background_height" android:layout_gravity="top" android:alpha="0" android:background="?attr/couiColorDivider" android:forceDarkAllowed="false" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <LinearLayout android:id="@+id/select_root_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginStart="@dimen/dimen_16dp" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="false" android:orientation="horizontal"> <TextView android:id="@+id/select_title_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/not_selected_file" android:textSize="16sp" android:lineHeight="22dp" android:textColor="?attr/couiColorLabelPrimary" android:textAppearance="@style/couiTextHeadlineXS" android:visibility="visible"/> <ImageView android:id="@+id/select_arraw_up" android:layout_width="18dp" android:layout_height="18dp" android:src="@drawable/arrow_up" android:layout_marginLeft="@dimen/dimen_6dp" android:visibility="gone" android:layout_gravity="center"/> </LinearLayout> <TextView android:id="@+id/select_body_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:lineHeight="20dp" android:textSize="14sp" android:textAppearance="?attr/couiTextBodyXS" android:textColor="?attr/couiColorLabelSecondary" android:visibility="gone" android:text="@string/selected_size"/> </LinearLayout> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1"/> <com.coui.appcompat.button.COUIButton android:id="@+id/btn_add_file" style="@style/Widget.COUI.Button.Large" android:layout_width="@dimen/dimen_96dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginEnd="@dimen/dimen_16dp" android:text="@string/label_add_recent_file" /> </LinearLayout> </FrameLayout> </FrameLayout>
07-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值