1

I have an activity with two EditText fields and "Add" button, something like this:

<EditText
    android:id="@+id/key"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:hint="Key"/>

<EditText
    android:id="@+id/value"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:hint="Value"/>

<Button
    android:id="@+id/add_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add"/>

In my Activity class I declare an array (its scope is activity's class) to store key-value pairs:

public class ExampleActivity extends AppCompatActivity {

    List<Pair<String, String>> paramsArray = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    //...

Everytime a user fills the fields and clicks the button a new entry is added to the array.

My question is:
could this activity be destroyed by Android OS (for example, because another app is currently running) and recreated with an empty array?

And in general how to store things like that within activity?

2 Answers 2

4

An Activity will be recreated with a simple rotation of the device, meaning you will lose this data if you don't properly save the state of your activity, or persist the data in a database.

In general you will want to save this data using the onSaveInstanceState(Bundle savedInstanceState) method, and retrieve the saved data when the Activity is recreated using the savedInstanceState parameter of the onCreate() method.

For more information read about Recreating an Activity.

Sign up to request clarification or add additional context in comments.

Comments

0

Data is stored only on runtime. Whenever you restart the app the array will be empty.

To store data permanently you can use a database or web server.

I didn't get your second question.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.