skip to Main Content

I am building an app which can allow people of the same interest to meet with each other. Now there is this page which consists of various interest (example – gaming, coding, singing, and sports) for the user to choose from.

The user can only choose one interest and they would be redirected to the particular interest main activity. But what’s happening is whenever I’m restarting the app I am seeing that interest page coming up (every time I open the app) what do I do so that I never see that page again after I chose one of the interest.

Example code would be so appreciated.

I recently got to know about sharedPreference, but I still have no idea where and how I would use it. What would be a detailed explanation if I have four activities, let’s say gameActivity, singActivity, sportsActivity, and codeActivity. How would I use sharepreference to never see the interest page again?

2

Answers


  1. User preferences like this are what shared preferences are designed for. They are already very well documented with examples here – https://developer.android.com/training/data-storage/shared-preferences

    The way you could use shared preferences to achieve the behaviour you describe could look something like this, but given I know little about how you’ve designed your app, it may not fit you well:

    const val GAMING_INTEREST = "gaming_interest"
    const val CODING_INTEREST = "coding_interest"
    ...
    
    val prefs = context.getPreferences(Context.MODE_PRIVATE)
    
    
    
    // On interests selected
    with (prefs.edit()) {
        putBoolean(GAMING_INTEREST, gamingInterestSelected)
        putBoolean(CODING_INTEREST, codingInterestSelected)
        ...
        apply()
    }
    
    
    
    // On app startup
    val interestedInGaming = prefs.getBoolean(GAMING_INTEREST, defaultValue)
    val interestedInCoding = prefs.getBoolean(CODING_INTEREST, defaultValue)
    ...
    
    when {
        interestedInGaming -> // Launch gameActivity
        interestedInCoding -> // Launch codeActivity
        ...
        else -> // Launch interests activity
    }
    

    Obviously these app startup checks need to go somewhere, so you can add a "StartupActivity" which doesn’t actually create any UI, just contains the logic for deciding which activity to launch next.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search