androidsharedpreferencesshowcaseview

how to save a function into sharedpreference


i want to save this view case into shared preference, so it will displayed only first time when apps running, here my code :

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_contact, menu);

    new Handler().postDelayed(
        new Runnable() {
            @Override
            public void run() {
                mFancyShowCaseView = new FancyShowCaseView.Builder(ContactTabActivity.this)
                        .focusOn(findViewById(R.id.item_sync)) // ActionBar menu item id
                        .focusCircleRadiusFactor(1.5)
                        .customView(R.layout.case_view_sync, new OnViewInflateListener() {
                            @Override
                            public void onViewInflated(@NonNull View view) {
                                view.findViewById(R.id.btnOke).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        mFancyShowCaseView.removeView(); // this doesn't work
                                    }
                                });
                            }
                        }).closeOnTouch(false)
                        .build();
                       mFancyShowCaseView .show();
            }
        }, 50
);
return super.onCreateOptionsMenu(menu);

}


Solution

  • You can't save function but you can save logic which will stop functions execution as in

    SharedPreferences pref;
    SharedPreferences.Editor editor;
    
     @Override
    public boolean onCreateOptionsMenu(Menu menu) {
         pref = getSharedPreferences(this,Context.MODE_PRIVATE);
         editor = pref.edit();
        if(pref.getBoolean("isFirstTime",true){   // default true for first time
    
         editor.putBoolean("isFirstTime",false).commit(); //<-- update  so it will false ever after
        getMenuInflater().inflate(R.menu.menu_contact, menu);
    
        new Handler().postDelayed(
            new Runnable() {
                @Override
                public void run() {
                    mFancyShowCaseView = new FancyShowCaseView.Builder(ContactTabActivity.this)
                            .focusOn(findViewById(R.id.item_sync)) // ActionBar menu item id
                            .focusCircleRadiusFactor(1.5)
                            .customView(R.layout.case_view_sync, new OnViewInflateListener() {
                                @Override
                                public void onViewInflated(@NonNull View view) {
                                    view.findViewById(R.id.btnOke).setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            mFancyShowCaseView.removeView(); // this doesn't work
                                        }
                                    });
                                }
                            }).closeOnTouch(false)
                            .build();
                           mFancyShowCaseView .show();
                }
            }, 50
       );
      }
      return super.onCreateOptionsMenu(menu);
    }