Android save the state of multiple dynamic inflated views


gold

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

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

Related


Android save the state of multiple dynamic inflated views

gold 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

Android save the state of multiple dynamic inflated views

gold 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

Android save the state of multiple dynamic inflated views

gold 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

Android save the state of multiple dynamic inflated views

gold 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

Android save the state of multiple dynamic inflated views

gold 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

Android save the state of multiple dynamic inflated views

gold 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

Android save the state of multiple dynamic inflated views

gold 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

Android save the state of multiple dynamic inflated views

gold 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

Android save the state of multiple dynamic inflated views

gold 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

Android save the state of multiple dynamic inflated views

gold 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

Android save the state of multiple dynamic inflated views

gold 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

Android save the state of multiple dynamic inflated views

gold 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

Android save the state of multiple dynamic inflated views

gold 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

Android save the state of multiple dynamic inflated views

gold 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

Android save the state of multiple dynamic inflated views

gold 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

Android save the state of multiple dynamic inflated views

Money 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

Android save the state of multiple CheckBoxes

xXJJJasonMokXx I have a lot of checkboxes and I want to save their state and restore them after exit. Here is the code for my list view adapter: public class ListViewAdapter extends BaseAdapter { Context context; AppPicker.Package[] packagesForAdapter

Android save the state of multiple CheckBoxes

xXJJJasonMokXx I have a lot of checkboxes and I want to save their state and restore them after exit. Here is the code for my list view adapter: public class ListViewAdapter extends BaseAdapter { Context context; AppPicker.Package[] packagesForAdapter

Save state of multiple dynamic components on ReactJS

Johnny Q Hi I have this component structure: <ScheduleApp /> <ScheduleForm /> <TeamField /> Mine ScheduleAppcontains a form called ScheduleForm, and under this form I have a field where the user can specify multiple teams, depending on how many teams ar

Save the state of a dynamic ViewPager

Benjamin Scharbau I have an Android application and I want to dynamically add and remove fragments to a ViewPager at runtime, and retain fragments when configuration changes. So far, I've implemented FragmentStatePagerAdapter to save Fragments in a list along

Save the state of a dynamic ViewPager

Benjamin Scharbau I have an Android application and I want to dynamically add and remove fragments to a ViewPager at runtime, and retain fragments when configuration changes. So far, I've implemented FragmentStatePagerAdapter to save Fragments in a list along

Create dynamic views in Android?

Teff In my application, I receive JSON multiple forms like this: "forms":[ {"name": "form1","title": "First Form","order": 1, "fields":[ "name": "firstName","type": "InputText", "maxLength": "10", "restriction

Combine nested views and multiple views into one state

JMK Ok, so I'm trying to use nested views and multiple views with angular-ui-router. I have the following HTML: Index.html <div ui-view="viewA"></div> viewA.html index.viewA <div ui-view="viewANested"></div> viewANested.html index.viewA.nested and the foll

Save Android application state

jlim: I understand how to save the state of the app using the methods described in the similar article ( Saving Android Activity State with Save Instance State ) like SharedPreferences, onSavedInstanceState() and onRestoreInstanceState() , but how do I save th

save activity state in android?

Ajinkya Sarata Hi all, I am ColorPickerin the app When I set the color selected by the background ColorPickerto the Activitybackground, it works. But when I restart the app, the colors change to default! How to save state Activity? Is it possible? Thanks in ad

Save Android application state

jlim: I understand how to save the state of the app using the methods described in the similar article ( Saving Android Activity State with Save Instance State ) like SharedPreferences, onSavedInstanceState() and onRestoreInstanceState() , but how do I save th

Android Exoplayer save state

Sahir Shaokian Hi I am using ExoPlayer to display video content in my VideoActivity. I use three variables to hold the state defined in the official documentation: playbackPosition, currentWindow, playWhenReady When the device is in landscape mode, I'm showing