Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a variable only once when app is created in Android?

Tags:

java

android

I am creating an android application. In my application, I need a variable that initializes only once when the application is first created. It should not be initialized even when the app gets updated. I added, "entered". So whenever I open the app I should have ArrayList which contains entered String.

ArrayList<String> strings=new ArrayList<String>();
strings.add("entered");
like image 444
Rocky Avatar asked Jan 25 '23 21:01

Rocky


1 Answers

As @blackapps mentioned You can use Shared Preferences

In MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();

    String yourVariable = sharedPref.getString(getString(R.string.nameOfValue), getString(R.string.defaultValue));

    if (yourVariable.equals(getString(R.string.defaultValue))) //app was run for 1st time
    {
        editor.putString(getString(R.string.nameOfValue), "String which You want to save"); //value that You want to put
        editor.apply();
    }
    else
    {
        // app wasn't run for 1st time
    }
}

In string.xml

<string name="nameOfValue">NAME_OF_VALUE</string>
<string name="defaultValue">DEFAULT_VALUE</string>

I saved String but You can save any type in Shared Preferences.


Edit:
To save ArrayList You can use GSON

First add dependency to grade and sync project:

implementstion 'com.google.code.gson:gson:2.8.6'

And then use this code:

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();

        boolean firstRun = sharedPref.getBoolean(getString(R.string.firstRun), true);
        ArrayList<String> arrayList;
        Gson gson = new Gson();

        if (firstRun) //app was run for 1st time
        {
            System.out.println("First run");

            // initialize array as You want
            arrayList = new ArrayList<>();
            arrayList.add("Some string");
            String arrayJSON = gson.toJson(arrayList);

            // add values to SharedPreferences
            editor.putString(getString(R.string.nameOfValue), arrayJSON);
            editor.putBoolean(getString(R.string.firstRun), false);

            // apply changes
            editor.apply();
        }
        else // app wasn't run for 1st time
        {
            System.out.println("Not first run");

            String arrayJSON = sharedPref.getString(getString(R.string.nameOfValue), getString(R.string.defaultValue));
            arrayList = gson.fromJson(arrayJSON, new TypeToken<ArrayList<String>>()
            {
            }.getType());
        }

        for (String s : arrayList)
        {
            System.out.println(s);
        }
    }
}
like image 170
iknow Avatar answered Jan 30 '23 00:01

iknow