如何将函数保存到 sharedpreference

how to save a function into sharedpreference

我想将此视图案例保存到共享首选项中,因此它只会在应用 运行 第一次显示时显示,这里是我的代码:

 @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);

}

您无法保存函数,但可以保存将停止函数执行的逻辑,如

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);
}