android 转屏幕事件保存数据--就一行代码
2012-04-01 20:23
369 查看
今天弄了一个下午,搞定了一件事情:让系统在转屏给我自动保存数据。
事情的缘由是:最近一个项目用到是全屏,里面的view是动态的变化的,一开始用的是台电的P73作为开发的,在锁屏解锁的时候系统是能够自动给我保存数据的,可是公司要用Samsung来做推广(是不是给我们老板是韩国的博士有关啊?)Samsung用的是GT-P1010,居然不能够自动的保存数据了。
对比两个Tab发现两个除了生产厂的不同,系统还不同国产的是2.3而韩国的是2.2 一开始就怀疑是不是2.2的不自动保存,然后就写程序来验证想法。要解决问题就我认为最好的哦方法是找到原因,为了找到原因我得有方向去寻找,我做的第一步就是让问题重现。
第一步,重现问题,写了一段代码:
在台电上只要是横屏的情况下 无论怎么锁屏解屏都是能够很好的哦保存数据的。在竖屏的情况是总是失去数据的。在Samsung上情况正好相反。
于是在就增加了代码中的7个经典的状态输出,看看到底是什么情况。输出的结果与Activity相同,在台电上横屏的是这个情况(本文只讨论解锁锁屏)
pause--resume而在竖屏的情况下确实这样的:
按下lock :pause--stop--destroy--create--start--restart--resume--pause
按下lock key:resume
解锁:pause--stop--destroy--create--start--resume
这样的话,在锁屏钱的一些操作数据都没有了,因为进行的是create还是两次!!在Samsung也有同样的情况只不过人家的是不同的方向罢了。
于是,现在就有解决方案了:
1、在onPause方法里面进行保存数据然后在onResume方法里回复数据
2、让系统给我们自动的保存恢复(根据前面说的,在台电上横屏的情况下是能够保存数据的)
我自认为我是一懒人,要是采取第一种方式来保存要写很多的代码--当然这应该是最常规的做法吧,毕竟doc要是推荐这样做的 Saving Persistent State推荐的也是这个做法,可是,我不想做这些东西,因为台电的能够给我保存,虽然只是在横屏/竖屏的情况下。
想到的第一个方案要找到屏幕旋转的原因,以及影响。
第一步禁用传感器,用
让我很郁闷!!看注释是这样的:
然后就在 manifest文件里增加onconfiChange属性为转屏的属性值,然后就奇迹般的好了。!!
然后就能够让系统给我自动的保存数据。我的一个下午就搞了这么多东西,有效代码居然只有一行
事情的缘由是:最近一个项目用到是全屏,里面的view是动态的变化的,一开始用的是台电的P73作为开发的,在锁屏解锁的时候系统是能够自动给我保存数据的,可是公司要用Samsung来做推广(是不是给我们老板是韩国的博士有关啊?)Samsung用的是GT-P1010,居然不能够自动的保存数据了。
对比两个Tab发现两个除了生产厂的不同,系统还不同国产的是2.3而韩国的是2.2 一开始就怀疑是不是2.2的不自动保存,然后就写程序来验证想法。要解决问题就我认为最好的哦方法是找到原因,为了找到原因我得有方向去寻找,我做的第一步就是让问题重现。
第一步,重现问题,写了一段代码:
public class MainActivity extends Activity { static MainActivity mainActivity; String data = "hello"; byte[] buf; private SensorManager mSensorManager; private Sensor mGravity = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mainActivity = this; setContentView(R.layout.main); mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); mGravity = mSensorManager.getDefaultSensor(Sensor.TYPE_ALL); mSensorManager.registerListener(null, mGravity, SensorManager.SENSOR_DELAY_NORMAL); data = "world"; System.out.println("mGravity: " + mGravity); // buf = new byte[1024 * 1024]; System.out.println("oncreate():" + System.currentTimeMillis()); } public static void view2(){ mainActivity.setContentView(R.layout.test_2); } @Override protected void onDestroy() { System.out.println("onDestroy():" + System.currentTimeMillis()); super.onDestroy(); } @Override protected void onStop() { System.out.println("onStop():" + System.currentTimeMillis()); super.onStop(); } @Override protected void onPause() { System.out.println("onPause():" + System.currentTimeMillis()); mSensorManager.registerListener(null, mGravity, SensorManager.SENSOR_DELAY_NORMAL); super.onPause(); } @Override protected void onResume() { System.out.println("onResume():" + System.currentTimeMillis()); super.onResume(); } @Override protected void onRestart() { System.out.println("onRestart():" + System.currentTimeMillis()); super.onRestart(); } @Override protected void onStart() { System.out.println("onStart():" + System.currentTimeMillis()); super.onStart(); } @Override public boolean onTouchEvent(MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ setContentView(R.layout.test_2); } return true; } }然后在分别在两个他Tab上运行,居然有时候能保存,有时不能保存,经过几次的实验发现是这样的情况
在台电上只要是横屏的情况下 无论怎么锁屏解屏都是能够很好的哦保存数据的。在竖屏的情况是总是失去数据的。在Samsung上情况正好相反。
于是在就增加了代码中的7个经典的状态输出,看看到底是什么情况。输出的结果与Activity相同,在台电上横屏的是这个情况(本文只讨论解锁锁屏)
pause--resume而在竖屏的情况下确实这样的:
按下lock :pause--stop--destroy--create--start--restart--resume--pause
按下lock key:resume
解锁:pause--stop--destroy--create--start--resume
这样的话,在锁屏钱的一些操作数据都没有了,因为进行的是create还是两次!!在Samsung也有同样的情况只不过人家的是不同的方向罢了。
于是,现在就有解决方案了:
1、在onPause方法里面进行保存数据然后在onResume方法里回复数据
2、让系统给我们自动的保存恢复(根据前面说的,在台电上横屏的情况下是能够保存数据的)
我自认为我是一懒人,要是采取第一种方式来保存要写很多的代码--当然这应该是最常规的做法吧,毕竟doc要是推荐这样做的 Saving Persistent State推荐的也是这个做法,可是,我不想做这些东西,因为台电的能够给我保存,虽然只是在横屏/竖屏的情况下。
想到的第一个方案要找到屏幕旋转的原因,以及影响。
第一步禁用传感器,用
android:screenOrientation="nosensor"来设置,发现还是不行--可能是没有禁用掉传感器的缘故于是就监听事件,想到以前看android哦源码的时候有一个叫做onConfigurationChanged的方法,就试试这个方法,直接在代码中增加override 发现居然没有直接调用!!!
@Override public void onConfigurationChanged(Configuration newConfig) { System.out.println("onConfigurationChanged: is called " + newConfig); newConfig.orientation = Configuration.ORIENTATION_PORTRAIT; super.onConfigurationChanged(newConfig); }
让我很郁闷!!看注释是这样的:
/** * Called by the system when the device configuration changes while your * activity is running. Note that this will <em>only</em> be called if * you have selected configurations you would like to handle with the * {@link android.R.attr#configChanges} attribute in your manifest. If * any configuration change occurs that is not selected to be reported * by that attribute, then instead of reporting it the system will stop * and restart the activity (to have it launched with the new * configuration). * * <p>At the time that this function has been called, your Resources * object will have been updated to return resource values matching the * new configuration. * * @param newConfig The new device configuration. */
然后就在 manifest文件里增加onconfiChange属性为转屏的属性值,然后就奇迹般的好了。!!
然后就能够让系统给我自动的保存数据。我的一个下午就搞了这么多东西,有效代码居然只有一行
相关文章推荐
- android 超简单的数据保存与读取 一行代码搞定数据保存与读取 ACache
- android基础笔记——模板代码:SharedPreferences保存、读取数据
- android stdio 设置屏幕旋转后保存数据
- 【Android】代码实现模拟屏幕点击和键盘按键事件
- Android-旋转屏幕前保存数据
- Android通过代码模拟物理、屏幕点击事件
- Android通过代码模拟物理、屏幕点击事件
- Android防止过快点击造成多次事件 一行代码搞定,特别简单,可以复用
- android之纵横屏幕切换时保存数据
- Android 一行代码解决菜单隐藏问题,又扔给你事件分发机制
- android之纵横屏幕切换时保存数据
- android 双击屏幕触发事件,放大图片,不用接入借口api,不用OnGestureListener,不造成代码累赘方法
- Android设置屏幕旋转后保存数据
- Android代码模拟物理、屏幕点击事件
- android之纵横屏幕切换时保存数据
- Android通过代码模拟物理、屏幕点击事件
- Android中RecyclerView的item中控件的点击事件添加删除一行、上移下移一行的代码实现
- Android通过代码模拟物理、屏幕点击事件
- android---(Activity组件、状态、生命周期、传递数据、处理返回结果、屏幕方向、保存数据)
- Android防止过快点击造成多次事件 一行代码搞定,特别简单,可以复用