恢复android中活动的动态UI元素

【恢复android中活动的动态UI元素】古之立大事者,不惟有超世之才,亦必有坚忍不拔之志。这篇文章主要讲述恢复android中活动的动态UI元素相关的知识,希望能为你提供帮助。
我一直在最高的投票回答Here和其他一些关于SO的答案。我正在尝试处理配置更改或任何调用onSaveInstanceState() / onRestoreInstanceState()的内容。到目前为止,我能够恢复TextViews(正确吗?),但是我遇到动态添加ui元素的问题,比如ImageButtons,在我的例子中,按钮没有恢复。这可能是一些愚蠢的事情,但我已经避免长时间寻求帮助了。我曾经在某处提到过将对象保存为捆绑......但这似乎更直接。感谢任何建议。
哦,我也只是在模拟器上测试,在横向/纵向之间旋转
编辑:更新了@ 18446744073709551615的工作示例和建议。感谢大家的帮助,看起来保存整个对象可能是更复杂代码的更好方法。
基本的xml布局:

< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > < TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello" /> < Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="set" android:text="Button" /> < Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="add" android:text="Button" /> < /LinearLayout>

这只是一个稍微修改过的空白活动:
package com.example.savestate; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends Activity {private static final String TAG = "MainActivity"; int btnId = 0; int myBtns; LinearLayout panel; TextView textView; String myString; ImageButton[] btnArray; @Override protected void onCreate(Bundle savedInstanceState) { Log.d(TAG, "begin of creation, myString is " + myString); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textView1); Log.d(TAG, "end of creation, myString is: " + myString); }@Override protected void onResume() { super.onResume(); textView.setText(myString); restoreBtns(); }@Override public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); savedInstanceState.putString("MyString", "Welcome back"); savedInstanceState.putInt("MyBtns", btnId); Log.d(TAG, "on save, myString is: " + myString); }@Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); myString = savedInstanceState.getString("MyString"); myBtns = savedInstanceState.getInt("MyBtns"); Log.d(TAG, "on restore, myBtns is: " + myBtns); Log.d(TAG, "on restore, myString is: " + myString); }public void restoreBtns() { if(myBtns > 0) { panel = (LinearLayout) findViewById(R.id.LinearLayout1); btnArray = new ImageButton[myBtns]; for (int i = 0; i < myBtns; i++){ btnId++; btnArray[i] = new ImageButton(this); btnArray[i].setImageResource(R.drawable.ic_launcher); btnArray[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); btnArray[i].setId(btnId); panel.addView(btnArray[i]); } }}public void add(View view) { btnId++; panel = (LinearLayout) findViewById(R.id.LinearLayout1); ImageButton imgBtn = new ImageButton(this); imgBtn.setImageResource(R.drawable.ic_launcher); imgBtn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); imgBtn.setId(btnId); panel.addView(imgBtn); }public void set(View view) { textView.setText("Goodbye"); Log.d(TAG, "on set(), myString is " + myString); }}

答案当onCreate()执行时,myBtns为0.由于if语句,restoreBtns()是一个无操作。然后,调用onStart(),之后调用onRestoreInstanceState():在onStart()之后调用此方法。因此myBtns仅在使用后才会初始化。
建议:将按钮内容移动到onResume()。
另一答案试试这种方式
< activity android:name="com.yourpackage.YourActivity" android:configChanges="keyboardHidden|orientation|screenSize" />


    推荐阅读