每次显示片段时都会出现 Showcaseview

Showcaseview appear every time when fragment shown

我正在尝试在片段中使用 showcaseview。它有效,但它不断出现在片段的每个开口处。我希望它在第一次打开时显示一次,再也不会显示。这是我的代码:

public class Map extends Fragment {
    public Map() {
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    private MapView mMapView;
    private GoogleMap mGoogleMap;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_map, container, false);
        mMapView = (MapView) view.findViewById(R.id.map_dashBoard);
        mMapView.onCreate(savedInstanceState);
        mMapView.onResume();

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }
        mGoogleMap = mMapView.getMap();

        new ShowcaseView.Builder(getActivity())
                .setTarget(new ViewTarget(view.findViewById(R.id.map_dashBoard)))
                .setContentTitle(R.string.map_context)
                .build();
        return view;
    }
} 

我该如何解决这个问题?

使用静态或共享首选项布尔值

    if(!getDefaults("shown",getActivity())){
    new ShowcaseView.Builder(getActivity())
                    .setTarget(new ViewTarget(view.findViewById(R.id.map_dashBoard)))
                    .setContentTitle(R.string.map_context)
                    .build();
setDefaults("shown",true,getActivity())
    }

 private void setDefaults(String key, boolean value, Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    private boolean getDefaults(String key, Context context) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        return preferences.getBoolean(key, false);
    }

替换:

new ShowcaseView.Builder(getActivity())
            .setTarget(new ViewTarget(view.findViewById(R.id.map_dashBoard)))
            .setContentTitle(R.string.map_context)
            .build();

通过使用共享首选项:

SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
boolean isShadowShown = prefs.getBoolean("shadowView", false);

if(isShadowShown){
new ShowcaseView.Builder(getActivity())
    .setTarget(new ViewTarget(view.findViewById(R.id.map_dashBoard)))
    .setContentTitle(R.string.map_context)
    .build();

SharedPreferences.Editor editor = getSharedPreferences("MyPrefs", MODE_PRIVATE).edit()
editor.putBoolean("shadowView", true);
editor.commit();
}

为此使用 SharedPreference:

if(!LoadPreferencesBoolean(context,"firsttime",false)){
new ShowcaseView.Builder(getActivity())
                .setTarget(new ViewTarget(view.findViewById(R.id.map_dashBoard)))
                .setContentTitle(R.string.map_context)
                .build();
SavePreferences(context,"firsttime",true)
}

public void SavePreferences(Context context,String key, Boolean value) {

    SharedPreferences.Editor editor =PreferenceManager.getDefaultSharedPreferences(context).edit();
    editor.putBoolean(key, value);
    editor.commit();
}

public boolean LoadPreferencesBoolean(Context context,String key, Boolean defaul) {
    boolean data = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(key, defaul);
    return data;
}