Android save the state of multiple dynamic inflated views
I'm trying to save the state of many dynamically inflated views (like keeping their values when changing screen orientation). I've looked around but still haven't found an answer to my problem.
XML of the item I'm inflating in the fragment
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border"
android:layout_marginTop="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/ingredient_name1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:hint="@string/ingredient_hint"
android:inputType="text" />
<Spinner
android:id="@+id/ingredient_category1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:entries="@array/categories" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/measure_amount1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:hint="@string/num_hint"
android:inputType="numberDecimal" />
<Spinner
android:id="@+id/measure_type1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:entries="@array/measurements" />
</LinearLayout>
</LinearLayout>
So I have a static variable in this snippet that counts how many static variables there are, and the ones greater than zero. Recreate the view in a loop to get as many views as possible:
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.new_recipe,container,false);
mainLayout = view.findViewById(R.id.ingredient_layout);
newIngredientButton = view.findViewById(R.id.new_ingredient);
ScrollView scrollView = view.findViewById(R.id.ingredient_sv);
//Re-create the items if we have more than 0
if (ingredientCount > 0) {
for (int i = 0; i < ingredientCount; i++) {
View newItem = LayoutInflater.from(getContext()).inflate(R.layout.ingredient_item,
mainLayout, false);
newItem.setId(i);
mainLayout.addView(newItem);
}
}
//OnClick to inflate the item
newIngredientButton.setOnClickListener(l -> {
View newItem = LayoutInflater.from(getContext()).inflate(R.layout.ingredient_item,
mainLayout, false);
newItem.setId(ingredientCount);
mainLayout.addView(newItem);
scrollView.post(() -> scrollView.fullScroll(View.FOCUS_DOWN));
ingredientCount++;
});
return view;
}
If I understand what you're trying to do, you shouldn't be doing it at all. In addition to tracking and recreating all views and data about screen orientation changes,
android:configChanges="orientation|screenSize"
It is easiest to add to the list under Activities . This prevents your app from restarting on orientation change, then you can handle all the changes you want to make in the method
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
...
}
Alternatively, you can keep the fragment and retrieve it on restart as described in this link
https://developer.android.com/guide/topics/resources/runtime-changes.html