1
我是Android中的法國noob,我有一個問題,我找不到解決方案。 實際上,我的平板電腦在旋轉期間無法保存我的LinearLayouts狀態。我已經嘗試了很多的東西,而且我做了這個代碼:當我打開我的平板電腦時,如何保存我的ImageView
public class Repas extends Activity {
private LinearLayout area1, area2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.repas);
area1 = (LinearLayout) findViewById(R.id.area1);
area2 = (LinearLayout) findViewById(R.id.area2);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
final int width_screen=dm.widthPixels;
area2.setMinimumWidth(width_screen);
if(savedInstanceState != null) {
int count_item = savedInstanceState.getInt("number_of_item");
String image = savedInstanceState.getString("Sauvegarde_repas0");
//area2.addView(image);
}
else {
TypedArray arrayResources = getResources().obtainTypedArray(
R.array.resicon);
for (int i = 0; i < arrayResources.length(); i++) {
ImageView imageView = new ImageView(this);
imageView.setImageDrawable(arrayResources.getDrawable(i));
imageView.setOnTouchListener(myOnTouchListener);
area2.addView(imageView);
}
arrayResources.recycle();
}
}
}
@Override
public void onSaveInstanceState(Bundle outState)
{
int count = area1.getChildCount();
View v = null;
outState.putInt("number_of_item", count);
for(int i=0; i<count; i++) {
v = area1.getChildAt(i);
outState.putString("Sauvegarde_repas"+i, v.toString());
}
super.onSaveInstanceState(outState);
}
我現在的問題,假設我的邏輯是正確的,是我無法將字符串轉換成的ImageView我的平板電腦的旋轉過程中。你可以幫我嗎 ?
我也想知道我做的是對的:我的意思是讓我的佈局的元素一個接一個(我沒有找到如何保存整個佈局)?
我希望我很清楚:如果您需要更多信息,請不要猶豫!
非常感謝您的幫助!
http://developer.android.com/guide/topics/resources/runtime-changes.htmlhttp://stackoverflow.com/questions/3915952/how-to-save-state-during-orientation-change-in -android-if-the-state-is-made-of-m http://stackoverflow.com/questions/12214600/save-data-and-change-orientation –
謝謝你的回答!正如你所看到的,我已經使用onSavedInstanceState .... – ValentinLoricourt