Android中关于横竖屏问题

news/2024/7/7 7:43:35

在以前的版本中只要在AndroidManifest.xml文件中对activity指定android:configChanges="keyboardHidden|orientation"属性,转屏的时候就会不再重新调用OnCreate()函数,而是调用onConfigurationChanged()。

 

但是在自从android3.2以后,再这样设置的话,会发现转屏后仍然会调用OnCreate(),而不是onConfigurationChanged();跟踪framework层代码,就会发现问题所在,是由于google在android3.2中添加了screensize改变的通知,在转屏的时候,不仅是orientation发生了改变,screensize同样也发生了改变,而在判断是调用onConfigurationChanged还是OnCreate时,采用的是如下判断:

 

int diff = activity.mCurrentConfig.diff(config);

if (diff != 0) {                

// If this activity doesn't handle any of the config changes then don't bother calling onConfigurationChanged as we'regoing to destroy it.

if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {

shouldChangeConfig = true;

}

}

 

public int getRealConfigChanged() {

return applicationInfo.targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB_MR2 ? (configChanges | ActivityInfo.CONFIG_SCREEN_SIZE

| ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE) : configChanges;

}

 

 

通过上面的分析,可发现有两种方法解决该问题:(只需要修改AndroidManifest.xml)

1.指定android:configChanges="keyboardHidden|orientation|screenSize",其他的代码和以前的代码一样处理;

2.在AndroidManifest.xml中指定targetSdkVersion为3.2以前的版本(3.2的版本号为13),系统会自动加上screenSize属性值。

   比如:<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="12" />  

 

转载地址:http://www.cnblogs.com/-cyb/articles/Android_onConfigurationChanged.html


http://www.niftyadmin.cn/n/3649061.html

相关文章

应用双节点,数据库是dg_如何使用节点构建轻量级发票应用程序:数据库和API

应用双节点,数据库是dg介绍 (Introduction) To get paid for goods and services provided, businesses need to send invoices to their customers informing them of the services that they will be charged for. Back then, people had paper invoices which they gave to …

查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现)

//search.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "LinkTable.h"#defineMAX_KEY 500//------------------------------数组实现部分----------------------------------/**//* 无序数组顺序查找算法…

网页开端第四次培训笔记

CSS常用属性设置 1.背景&#xff08;css背景属性用于定义HTML元素的背景效果 background-color 设置元素的背景效果background-image 设置元素的背景图像&#xff0c;默认情况下&#xff0c;背景图像进行平铺重复显示&#xff0c; …

父子节点 构建_如何使用节点构建轻量级发票应用程序:用户界面

父子节点 构建介绍 (Introduction) In the first part of this series, you set up the backend server for the invoicing application. In this tutorial you will build the part of the application that users will interact with, known as the user interface. 在本系列…

Android零碎小知识

获取当前的版本号 public double getVersionCode() {try {int versionCode getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;return versionCode;} catch (PackageManager.NameNotFoundException e) {e.printStackTrace();}return 0; }将版本号设置成1.…

TabActivity实现多页显示效果

由于手机屏幕有限&#xff0c;所以我们要尽量充分利用屏幕资源。在我们的应用程序中通常有多个Activity&#xff0c;而且会经常切换显示&#xff0c;这样我们就可以用TabActivity来显示。其效果如图1所示。 图1 tabActivity显示效果 本文就来研究TabActivity。根据帮助文档的解…

网页开端第五次培训笔记

JavaScript简介 JavaScript是一种具有面向对象能力的、解释型的程序设计语言。更具体点&#xff0c;它是基于对象和时间驱动并具有相对安全性的客户端脚本语言。它的主要目的是&#xff0c;验证发往服务器端的数据、增加Web互动、加强用户体验度等。 JavaScript的组成 ECMASc…

react中使用构建缓存_如何使用React,GraphQL和Okta构建健康跟踪应用

react中使用构建缓存介绍 (Introduction) In this tutorial, you will build a health tracking app using GraphQL API with Vesper framework, TypeORM, and MySQL as a database. These are Node frameworks, and you’ll use TypeScript for the language. For the client,…