TourGuide icon indicating copy to clipboard operation
TourGuide copied to clipboard

Ability to display TourGuide only once then disable it.

Open TommyAsAService opened this issue 9 years ago • 2 comments

Tour only display when open app for the first time

TommyAsAService avatar Jul 30 '15 13:07 TommyAsAService

??? i dont know if its my english that is poor or your question is a little confuse!

you want only once? add a SharedPreference

open app: check if SharedPreferences exist or its false; if(false) { show TourGuide() set the SharedPreference to TRUE(already displayed) }

that this will open only once on the first run...

if you want to open more than once(everytime), just call the .playOn like the tutorial

defaultbr avatar Aug 01 '15 20:08 defaultbr

I also need that feature. But I do not like the if-else-dance suggested by @defaultbr each time using a TourGuide. So I extended this library by my own. Feel free to use this snippet if it helps.

import android.app.Activity;
import android.content.Context;
import android.view.View;

import tourguide.tourguide.TourGuide;

/**
 * Created by clemens on 22.06.17.
 */
public class FooTourGuide extends TourGuide {

    private static final String TOUR_GUIDE_PREFS = "com.example.foo.foo_tour_guide";
    private Activity mActivity;
    private String mSingleShotId;
    private boolean mForceShow;
    private boolean mPresent;

    public FooTourGuide(Activity activity) {
        super(activity);
        this.mActivity = activity;
    }

    public static FooTourGuide init(Activity activity){
        return new FooTourGuide(activity);
    }

    /**
     * Shows the TourGuide only once.
     * @param id a unique ID for this TourGuide.
     */
    public FooTourGuide singleShot(String id) {
        this.mSingleShotId = id;
        return this;
    }

    /**
     * Forces the TourGuide to show up, overrides {@link #singleShot(String)}.
     * @param force
     */
    public FooTourGuide forceShow(boolean force) {
        this.mForceShow = force;
        return this;
    }

    private boolean alreadyShowed() {
        return mActivity.getSharedPreferences(TOUR_GUIDE_PREFS, Context.MODE_PRIVATE)
                .getBoolean(mSingleShotId, false);
    }

    public FooTourGuide playOn(View view) {
        if (this.mForceShow) {
            // force showing (useful during dev)
            showTourGuide(view);
        } else if (! alreadyShowed()) {
            showTourGuide(view);
            // mark as shown
            mActivity.getSharedPreferences(TOUR_GUIDE_PREFS, Context.MODE_PRIVATE)
                    .edit()
                    .putBoolean(mSingleShotId, true)
                    .apply();
        }
        return this;
    }

    private void showTourGuide(View view) {
        super.playOn(view);
        this.mPresent = true;
    }

    public void cleanUp() {
        if (mPresent) {
            super.cleanUp();
            this.mPresent = false;
        } else {
            // nothing to do, do not call super.cleanUp()
            // avoids possible NPE at TourGuide#cleanUp().
        }
    }
}

Example usage:

FooTourGuide
                .init(getActivity())
                .singleShot("special-use-case")
                .setPointer(null)
                .setToolTip(new ToolTip()
                        .setTitle(getString(R.string.special_use_case_title))
                        .setDescription(getString(R.string.special_use_case_text)))
                .setOverlay(null)
                .playOn(target);

cleemansen avatar Jun 22 '17 13:06 cleemansen