Skip to main content

Making data persistent in android


In my application,there are some application specific settings, which should be available to me , next time when my application starts up.



In other words i want the data to be available across the sessions of an application cycle.



Can this be achieved without using database(sqlite).


Source: Tips4allCCNA FINAL EXAM

Comments

  1. Many applications may provide a way to capture user preferences on the settings of a specific application or an activity. For supporting this, Android provides a simple set of APIs.

    Preferences are typically name value pairs. They can be stored as “Shared Preferences” across various activities in an application (note currently it cannot be shared across processes). Or it can be something that needs to be stored specific to an activity.


    Shared Preferences: The shared preferences can be used by all the components (activities, services etc) off the applications.
    Activity handled preferences: These preferences can only be used with in the activity and can not be used by other components of the application.


    Shared Preferences:

    The shared preferences are managed with the help of the getSharedPreferences method of the Context class. The preferences are stored in a default file(1) or you can specify a file name(2) to be used to refer to the preferences.

    (1) Here is how you get the instance when you specify the file name

    public static final String PREF_FILE_NAME = "PrefFile";
    SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);


    MODE_PRIVATE is the operating mode for the preferences. It is the default mode and means the created file will be accessed by only the calling application. Other two mode supported are MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE. In MODE_WORLD_READABLE other application can read the created file but can not modify it. In case of MODE_WORLD_WRITEABLE other applications also have write permissions for the created file.

    (2) The recommended way is to use by the default mode, without specifying the file name:

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);


    Finally, once you have the preferences instance, here is how you can retrieve the stored values from the preferences:

    int storedPreference = preferences.getInt("storedInt", 0);


    To store values in the preference file SharedPreference.Editor object has to be used. Editor is the nested interface of the SharedPreference class.

    SharedPreferences.Editor editor = preferences.edit();
    editor.putInt("storedInt", storedPreference); // value to store
    editor.commit();


    Editor also support methods like remove() and clear() to delete the preference value from the file.

    Activity Preferences:

    The shared preferences can be used by other application components. But if you do not need to share the preferences with other components and want to have activities private preferences. You can do that with the help of getPreferences() method of the activity. The getPreference method uses the getSharedPreferences() method with the name of the activity class for the preference file name.

    Following is the code to get preferences:

    SharedPreferences preferences = getPreferences(MODE_PRIVATE);
    int storedPreference = preferences.getInt("storedInt", 0);


    The code to store values is also same as in case of shared preferences.

    SharedPreferences preferences = getPreference(MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putInt("storedInt", storedPreference); // value to store
    editor.commit();


    You can also use other methods like storing the activity state in database. Note Android also contains a package called android.preference. The package defines classes to implement application preferences UI.

    To see some more examples check Android's Data Storage post on developers site.

    ReplyDelete
  2. You should use shared preferences:

    SharedPreferences prefs = PreferencesManager.getDefaultSharedPreferences(context);
    prefs.edit().putString("my_pref", "my_value").commit();


    And to retrieve it:

    String value = prefs.getString("my_pref", "default Value");


    These preferences are persistent, and are also integrated with the preference activities.

    ReplyDelete
  3. Please read about Data Storage in the dev guide.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex