Activity转屏重建之 Activity.onConfigurationChanged

2024-09-06 09:08

本文主要是介绍Activity转屏重建之 Activity.onConfigurationChanged,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

偶尔也会遇到由于转屏引起的一些问题。

有些时候,并不希望由于转屏使得Activity取重建。

再如键盘消失后的重建。

下面以一个demo为例子,小小总结一下用法。

如果想在转屏后,屏幕上立马打印出当前处于什么横竖屏状态


1.都知道有个属性android:configChanges可以用来定义什么情况下可以使得Activity不会restart。

android:configChanges="screenSize|orientation"

2.下面再来看看用法

首先是AndroidManifest.xml,有两个点需要注意,1是添加权限,2是添加在哪些因素改变后希望不被重建。

<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>
<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="screenSize|orientation"
        android:theme="@style/AppTheme.NoActionBar">

其次是,需要复写Activity的onConfigurationChanged方法,假如在横竖屏切换后,需要改变的配置。

    @Override
    public void onConfigurationChanged(Configuration newConfig) {super.onConfigurationChanged(newConfig);
        Log.d(TAG_, "onConfigurationChanged --");
        int orientation = getResources().getConfiguration().orientation;
        mainContentTxv.setText("current orientaion is " + orientation);
//        super.onConfigurationChanged(newConfig);
    }

备注:

起始,只加了orientation,发现并不能达到转屏后实时屏幕显示横竖屏信息。

原因是,当转屏后,可供Activity使用的screenSize也发生了变化,如果只限定为orientation,并不能达到效果。这个最小变化的子集,需要关注。


3.Activity生命周期

当加了上述限制后,Activity在发生转屏后,将只调用onConfigurationChanged方法继续运行,其他回调方法不会发生。

在每个回调中打印相应的log,验证了这一点,结果如下:

01-07 09:21:25.173 23427 23427 D K2K     : onCreate --
01-07 09:21:25.225 23427 23427 D K2K     : onStart --
01-07 09:21:25.228 23427 23427 D K2K     : onResume --
//横屏
01-07 09:21:36.687 23427 23427 D K2K     : onConfigurationChanged --
//竖屏
01-07 09:21:40.112 23427 23427 D K2K     : onConfigurationChanged --
//横屏
01-07 09:21:43.635 23427 23427 D K2K     : onConfigurationChanged --
//返回launcher//back
01-07 09:21:47.134 23427 23427 D K2K     : onPause --
01-07 09:21:48.233 23427 23427 D K2K     : onStop --
01-07 09:21:48.234 23427 23427 D K2K     : onDestory --


附录:

Android API原文描述:

Specify one or more configuration changes that the activity will handle itself. If not specified, the activity will be restarted if any of these configuration changes happen in the system. Otherwise, the activity will remain running and itsActivity.onConfigurationChanged method called with the new configuration.

Note that all of these configuration changes can impact the resource values seen by the application, so you will generally need to re-retrieve all resources (including view layouts, drawables, etc) to correctly handle any configuration change.

These values must be kept in sync with those in ActivityInfo and include/utils/ResourceTypes.h.           


Must be one or more (separated by '|') of the following constant values.

ConstantValueDescription
mcc0x0001The IMSI MCC has changed, that is a SIM has been detected and updated the Mobile Country Code.
mnc0x0002The IMSI MNC has changed, that is a SIM has been detected and updated the Mobile Network Code.
locale0x0004The locale has changed, that is the user has selected a new language that text should be displayed in.
touchscreen0x0008The touchscreen has changed. Should never normally happen.
keyboard0x0010The keyboard type has changed, for example the user has plugged in an external keyboard.
keyboardHidden0x0020The keyboard or navigation accessibility has changed, for example the user has slid the keyboard out to expose it. Note that despite its name, this applied to any accessibility: keyboard or navigation.
navigation0x0040The navigation type has changed. Should never normally happen.
orientation0x0080The screen orientation has changed, that is the user has rotated the device.
screenLayout0x0100The screen layout has changed. This might be caused by a different display being activated.
uiMode0x0200The global user interface mode has changed. For example, going in or out of car mode, night mode changing, etc.
screenSize0x0400The current available screen size has changed. If applications don't target at leastHONEYCOMB_MR2 then the activity will always handle this itself (the change will not result in a restart). This represents a change in the currently available size, so will change when the user switches between landscape and portrait.
smallestScreenSize0x0800The physical screen size has changed. If applications don't target at least HONEYCOMB_MR2 then the activity will always handle this itself (the change will not result in a restart). This represents a change in size regardless of orientation, so will only change when the actual physical screen size has changed such as switching to an external display.
layoutDirection0x2000The layout direction has changed. For example going from LTR to RTL.
fontScale0x40000000The font scaling factor has changed, that is the user has selected a new global font size.


这篇关于Activity转屏重建之 Activity.onConfigurationChanged的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1141591

相关文章

LeakCanary测试app内存泄露+registerActivityLifecycleCallbacks管理Activity的生命周期

public class MyApplication extends Application {private String tag = "MyApplication";private static Stack<Activity> activityStack;//检测内存泄露private RefWatcher refWatcher;@Overridepublic void onCreate()

android studio怎么修改创建的Activity的模板?

1.找到android studio的安装目录,如下图,找到对应文件,修改EmptyActivity的模板。 2.修改成如下: 3.新建的EmptyActivity的默认代码如下:

Python(TensorFlow和PyTorch)两种显微镜成像重建算法模型(显微镜学)

🎯要点 🎯受激发射损耗显微镜算法模型:🖊恢复嘈杂二维和三维图像 | 🖊模型架构:恢复上下文信息和超分辨率图像 | 🖊使用嘈杂和高信噪比的图像训练模型 | 🖊准备半合成训练集 | 🖊优化沙邦尼尔损失和边缘损失 | 🖊使用峰值信噪比、归一化均方误差和多尺度结构相似性指数量化结果 | 🎯训练荧光显微镜模型和对抗网络图形转换模型 🍪语言内容分比 🍇Python图像归一化

JD 1385:重建二叉树

OJ题目:click here~~ 题目分析:给前序遍历序列和中序遍历序列,重构二叉树并输出后序遍历序列 剑指offer 面试题6 AC_CODE int pre[1008] , in[1008] ;struct Node{int x ;Node *left ;Node *right ;};bool buildsubtree(Node*& root , int* spre , in

查看当前正在运行的Activity列表

1、在终端中输入, 注意adb如果没有配置到系统环境变量里,则需要cd到它所在的文件夹下 “adb shell dumpsys activity” 2、出来的结果比较多,直接搜索"Running" ,找到 Running activities (most recent first):       TaskRecord{4069c020 #4 A shy.luo.ta

Activity被回收导致其内部的Fragment

方式一:   MainActivity重写onSaveInstanceState方法,将super.onSaveInstanceState(outState);注释掉,让其不再保存Fragment的状态,达到其随着MainActivity一起被回收的效果! 方式二: @Overrideprotected void onSaveInstanceState(Bundle outSta

OEM Activity Summary AAS 不记录后台进程 比如 rman

OEM中rman 备份认为是后台进程,所以在Activity Summary未显示 ,AAS 可以显示 去掉后台进程rman 一个也不显示!

Unable to instantiate activity

报的错, 04-25 20:56:13.629: E/Trace(1790): error opening trace file: No such file or directory (2) 04-25 20:56:13.653: E/AndroidRuntime(1790): FATAL EXCEPTION: main 04-25 20:56:13.653: E/AndroidRunt

Activity状态保存【onSaveInstanceState和onRestoreInstanceState】

Activity的onSaveInstanceState() 和 onRestoreInstanceState()方法: 1. 基本作用:   Activity的 onSaveInstanceState() 和 onRestoreInstanceState()并不是生命周期方法,它们不同于 onCreate()、onPause()

Android - 总结Activity与Fragment打开的时候获取组件的大小

Activity中获取 组件大小        代码: @Overridepublic void onWindowFocusChanged(boolean hasFocus) {// TODO Auto-generated method stubsuper.onWindowFocusChanged(hasFocus);Log.d("TAG", "A-button1-->"+button1);L