Kim
Kim

Reputation: 69

Android saving state of multiple dynamic inflated views

I'm trying to save the state of many dynamically inflated views (as in keep their values upon change in screen orientatien). I've looked all around, but yet not managed to find an answer to my problem.

My XML for the item I inflate 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 fragment that keeps count of how many there are and if there are more than 0. Views are re-created in a loop to get back as many as I had :

@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;
}

Upvotes: 1

Views: 81

Answers (1)

Trevor
Trevor

Reputation: 1451

If I understand what you're trying to do, you shouldn't be doing that at all. Instead of tracking and recreating all your views and data on changes in screen orientation,

It would be easiest to add android:configChanges="orientation|screenSize" to your manifest under activity. This prevents your app from restarting on orientation changes and then you can handle any changes you do want to make in the method

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        ...
    }

or alternatively you can retain the fragment and retrieve it upon restart as also described in this link

https://developer.android.com/guide/topics/resources/runtime-changes.html

Upvotes: 1

Related Questions