手柄屏幕旋转而不会丢失数据 - 机器人 [英] Handle screen rotation without losing data - Android
问题描述
我变得疯狂搞清楚什么是处理屏幕旋转的最佳方式。我看了几百个问题在这里/答案,但我真的很困惑。
我如何保存MyClass的数据重新创建活动之前,这样我就可以把一切都重新绘制活动,无须另外一个无用的初始化?
有没有比parcelable一个更清洁,更好的办法?
我需要处理的旋转,因为我想改变布局横向模式。
公共类MtgoLifecounterActivity延伸活动{
MyClass的MyClass的;
//第一次创建活动时调用
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
如果(??第一次运行... MyClass的== NULL?){
MyClass的=新MyClass的();
} 其他 {
//做其他的东西,但我需要MyClass的istance所有值。
}
//我想,这是所谓的只有第一次。
//则在发生屏幕的旋转,我想要恢复myClass的其他实例,
//充满数据。
}
可以使用覆盖方法的onSaveInstanceState()
和 onRestoreInstanceState()
。
或停止呼叫的onCreate()
在屏幕旋转刚加入这一行你的清单XML 安卓configChanges =keyboardHidden |定位
请注意:您的自定义类必须农具 Parcelable
下面的例子
@覆盖
公共无效的onSaveInstanceState(包outState){
super.onSaveInstanceState(outState);
outState.putParcelable(目标文件,MyClass的);
}
@覆盖
保护无效onRestoreInstanceState(包savedInstanceState){
// TODO自动生成方法存根
super.onRestoreInstanceState(savedInstanceState);
MyClass的= savedInstanceState.getParcelable(目标文件));
}
公共类MyClass的实现Parcelable {
私人诠释MDATA;
公众诠释describeContents(){
返回0;
}
/ **保存对象包裹* /
公共无效writeToParcel(包裹出来,诠释标志){
out.writeInt(MDATA);
}
公共静态最终Parcelable.Creator< MyParcelable> CREATOR
=新Parcelable.Creator< MyParcelable>(){
公共MyParcelable createFromParcel(包裹中){
返回新MyParcelable(中);
}
公共MyParcelable [] newArray(INT尺寸){
返回新MyParcelable【尺寸】;
}
};
/ **从包裹*重建对象/
私人MyParcelable(包裹中){
MDATA = in.readInt();
}
}
I'm becoming crazy figuring out what is the best way to handle screen rotation. I read hundreds of questions/answers here but I'm really confused.
How can I save myClass data before the activity is re-created so I can keep everything for redrawing activity without another useless initialization?
Is there a cleaner and better way than parcelable?
I need to handle rotation because I want to change layout in Landscape mode.
public class MtgoLifecounterActivity extends Activity {
MyClass myClass;
// Called when the activity is first created
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
If ( ?? first run...myClass == null ? ) {
myClass = new MyClass();
} else {
// do other stuff but I need myClass istance with all values.
}
// I want that this is called only first time.
// then in case of rotation of screen, i want to restore the other instance of myClass which
// is full of data.
}
can use override method onSaveInstanceState()
and onRestoreInstanceState()
.
or to stop calling onCreate()
on screen rotation just add this line in your manifest xml android:configChanges="keyboardHidden|orientation"
note: your custom class must implements Parcelable
example below.
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable("obj", myClass);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
myClass=savedInstanceState.getParcelable("obj"));
}
public class MyClass implements Parcelable {
private int mData;
public int describeContents() {
return 0;
}
/** save object in parcel */
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mData);
}
public static final Parcelable.Creator<MyParcelable> CREATOR
= new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
/** recreate object from parcel */
private MyParcelable(Parcel in) {
mData = in.readInt();
}
}
这篇关于手柄屏幕旋转而不会丢失数据 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!