android 转屏幕事件保存数据--就一行代码

今天弄了一个下午,搞定了一件事情:让系统在转屏给我自动保存数据。

事情的缘由是:最近一个项目用到是全屏,里面的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属性为转屏的属性值,然后就奇迹般的好了。!!

然后就能够让系统给我自动的保存数据。我的一个下午就搞了这么多东西,有效代码居然只有一行
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值