Android, Как я могу получить последнее состояние активности?
У меня есть one.xml
файл:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollView">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:orientation="vertical"
android:id="@+id/lineerLayout">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/sp_info"
android:id="@+id/label_sp_info"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/label_x" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:id="@+id/spx"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/label_y" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:id="@+id/spy" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_label_continue"
android:id="@+id/continue_aligment" />
</LinearLayout>
</ScrollView>
и тогда у меня есть two.xml
файл как это
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/nPointlinearLayout"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/nPointInfo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/labelnPointX"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:id="@+id/nPointX" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/labelnPointY" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/nPointY" />
</LinearLayout>
Я могу добавить two.xml в one.xml, когда нажимаю кнопку, используя inflater. Нет проблем. Но когда я нажимаю кнопку возврата телефона или завершаю все действия, а затем снова ввод, добавление просмотров исчезло. Как я могу получить последнее состояние активности? со всеми удостоверениями личности?
это мой код для нажатия кнопки.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
continueAligment=(Button)findViewById(R.id.continue_aligment);
lineerLayout=(LinearLayout)findViewById(R.id.lineerLayout);
inflater=getLayoutInflater();
continueAligment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
view=inflater.inflate(R.layout.two,lineerLayout,false);
nPointLineerLayout=(LinearLayout)view.findViewById(
R.id.nPointlinearLayout);
lineerLayout.addView(nPointLineerLayout,
lineerLayout.indexOfChild(continueAligment));
}
});
}
1 ответ
Решение
Добавить значение в общих настройках
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
continueAligment = (Button) findViewById(R.id.continue_aligment);
lineerLayout = (LinearLayout) findViewById(R.id.lineerLayout);
final SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
inflater = getLayoutInflater();
if (sp.getBoolean("ADD_TWO", false)) {
inflateTwo();
} else continueAligment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
inflateTwo();
sp.edit().putBoolean("ADD_TWO", true).commit();
}
});
}
private void inflateTwo() {
view = inflater.inflate(R.layout.two, lineerLayout, false);
nPointLineerLayout = (LinearLayout) view
.findViewById(R.id.nPointlinearLayout);
lineerLayout.addView(nPointLineerLayout,
lineerLayout.indexOfChild(continueAligment));
}
Конечно, если вы уже добавили два, вы хотите отключить эту кнопку - нет?