yoshi24
yoshi24

Reputation: 3177

How to save String[] persistantly?

I am trying to save

public   String [] myRemoteImages = {imageUrl,imageUrl2,imageUrl3,imageUrl4};

Persistantly.

I want to save the variables inside the array list, to a persistant storage so when the app is killed or destroyed. the URL's are still availible. But the url's will change about every month. I dont want to keep building up strings in a database over time.

So whats the best way to go about doing this?

EDIT: Something like...

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.v("onSavedInstanceState", "Saved inside the bundle");
        SharedPreferences app_preferences = 
                PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = app_preferences.edit();
        editor.putString("URL1", imageUrl);
        editor.putString("URL2", imageUrl2)

    }

Upvotes: 1

Views: 157

Answers (3)

Paul Sasik
Paul Sasik

Reputation: 81429

You have a number of data storage options available for Android, see list of options and overview here, with the simplest one being Shared Preferences. If you're just going to keep a handful of URLs around this is probably your best bet. The cleanup is up to you but can be very simple with a proper key/value pair naming strategy.

E.g.:

"URL1", "google.com"
"URL2", "stackoverflow.com"
...
"URLn", "nthwebsite.com"

where the keys (URLn) would remain the same but the web pages changed as necessary.

Upvotes: 4

dongshengcn
dongshengcn

Reputation: 6504

Or you can save it to SharedPreference if you do not want to use DB

Upvotes: 1

Matt Ball
Matt Ball

Reputation: 359776

Save it in the application's SQLite instance, and manually clean up or overwrite old URLs.

Upvotes: 2

Related Questions