Search code examples
androidrealmtarget

Open TapTarget only on first use of application


I have a TapTargetwhen my app opens, I only want it to open on the very first launch of the app, how do I store a value to make sure it doesn't open again on other launches, this is what I tried but its not working, the TapTarget opens every time the app launches.

Code:

realm.executeTransaction { realm ->
        val result = Taptarget()
        result.cal = ""
        result.chat = ""
        result.depfpsc = ""
        result.info = ""
        result.module = ""
        realm.insert(result)

    }

    realm.executeTransaction { realm ->
        val result = realm.where(Taptarget::class.java).findFirst()!!
        if(result.module == "Y")
        {

        }
        else
        {
            if (mFabPrompt != null) {
                return@executeTransaction
            }
            mFabPrompt = MaterialTapTargetPrompt.Builder(this@MainActivity)
                    .setTarget(findViewById<View>(R.id.navigation_modules))
                    .setPrimaryText("Send your first email")
                    .setSecondaryText("Tap the envelope to start composing your first email")
                    .setIconDrawable(resources.getDrawable(R.drawable.ic_folder_black_24dp))
                    .setBackgroundColour(resources.getColor(R.color.colorAccentTrans))
                    .setAnimationInterpolator(FastOutSlowInInterpolator())
                    .setPromptStateChangeListener { prompt, state ->
                        if (state == MaterialTapTargetPrompt.STATE_FOCAL_PRESSED || state == MaterialTapTargetPrompt.STATE_DISMISSING) {
                            mFabPrompt = null
                        }
                    }
                    .create()
            mFabPrompt!!.show()
            result.depfpsc = "Y"
            realm.insertOrUpdate(result!!)
        }
    }

Solution

  • I am not sure in what language you write the code. But let me explain in java in which i am proficient. You can easily adapt this to any other language.

    First create a class PreferencesManager like this:

    public class PreferencesManager {
    
    SharedPreferences pref;
    SharedPreferences.Editor editor;
    Context _context;
    
    // shared pref mode
    int PRIVATE_MODE = 0;
    
    // Shared preferences file name
    private static final String PREF_NAME = "splash-welcome";
    
    // Shared preference variable name
    private static final String IS_FIRST_TIME_LAUNCH = "IsFirstTimeLaunch";
    
    // Constructor
    public PrefManager(Context context) {
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }
    
    // This method to be used as soon as the fist time launch is completed to update the 
    // shared preference
    public void setFirstTimeLaunch(boolean isFirstTime) {
        editor.putBoolean(IS_FIRST_TIME_LAUNCH, isFirstTime);
        editor.commit();
    }
    
    // This method will return true of the app is launched for the first time. false if 
    // launched already 
    public boolean isFirstTimeLaunch() {
        return pref.getBoolean(IS_FIRST_TIME_LAUNCH, true);
    }
    }
    

    Now in every activity, you have to check if the app is being launched for the first time:

     PreferencesManager preferencesManager = new PreferencesManager (this);
     if (!preferencesManager.isFirstTimeLaunch()) {
        // Set shared preference value to false so that this block will not be called 
        // again until your user clear data or uninstall the app
        preferencesManager.setFirstTimeLaunch(false); 
        // Write your logic here
        }
    

    This might not be the exact answer you are looking for. But this may point you in right direction :)