android快速旋转切换
使用 onRetainNonConfigurationInstance()
,实现快速旋转
例子如下:
public class ACompatibilityActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(getLastNonConfigurationInstance()==null){
System.out.println("first creat? Yes");
}else{
System.out.println("first creat? No");
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("ACompatibilityActivity.onDestroy()");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
System.out.println("ACompatibilityActivity.onPause()");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
System.out.println("ACompatibilityActivity.onStop()");
}
ArrayList<String> retainData;
//这里为简单的数据加载
@Override
public Object onRetainNonConfigurationInstance() {
super.onRetainNonConfigurationInstance();
//会在第一次onCreat()的时候调用
//旋转后通过调用getLastNonConfigurationInstance()返回是否为null来决定是否还进行相关的数据加载
retainData = new ArrayList<String>();
retainData.add("1");
retainData.add("2");
retainData.add("3");
retainData.add("4");
retainData.add("5");
retainData.add("6");
return retainData;
}
}