• Latest Code...

    Share Preferences in Kotlin

    Interface for accessing and modifying preference data returned by getSharedPreferences(String, int).
    You can create a new shared preference file or access an existing one by calling one of two methods:
    • getSharedPreferences() — Use this if you need multiple shared preference files identified by name, which you specify with the first parameter. You can call this from any Context in your app.
    • getPreferences() — Use this from an Activity if you need to use only one shared preference file for the activity. Because this retrieves a default shared preference file that belongs to the activity, you don't need to supply a name.
    Lets to it in kotlin:

    First attempt to defined the shared preference and editor then put values on editor and apply for use.
    What we have doing in MainActivity:

    Now, get the value from shared preference in second activity using key.

    Simple we can use for login section, where we can save username, password or remember password for any user.

    Here is sample output screenshot:

    Download full code and fork

    #HappyCoding!!! and #IloveKotlin

    No comments:

    Post a Comment


    Most recent Elastic Search tutorials for beginner, intermediate and advance level developer, Yubraj Pokharel, in CodingWorkspace