Sharedpreferences 在 onCreateOptionsMenu 中不起作用

Sharedpreferences doesn't work in onCreateOptionsMenu

我尝试显示指向菜单项的展示视图,但此共享首选项只是在我的应用程序关闭之前保存,在我关闭我的应用程序并再次打开后展示视图将再次出现。我如何才能仅在第一次显示展示视图?

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

    pref = getSharedPreferences(String.valueOf(getApplicationContext()), 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
        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)
                                .focusBorderSize(15)
                                .focusBorderColor(Color.parseColor("#FFA64D"))
                                .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();
                                            }
                                        });
                                    }
                                }).closeOnTouch(false)
                                .build();
                        mFancyShowCaseView.show();
                    }
                }, 50
        );
    }
    editor.commit();
    return super.onCreateOptionsMenu(menu);
}

代替

pref = getSharedPreferences(String.valueOf(getApplicationContext()), Context.MODE_PRIVATE);

使用

pref = getSharedPreferences("First_tym_check",Context.MODE_PRIVATE);

这是因为:-

String.valueOf(getApplicationContext()) 这个字符串值是 not CONSTANT

如果您重新启动应用程序,您会注意到该值会发生显着变化,例如:-

  • (yourPackageName).@521c1,下次,
  • (yourPackageName).@631d1 ,

So, the string value is different every time .

使用下面的代码。使用 apply() 而不是 commit() 和 getApplicationContext().getPackageName() 而不是 getApplicationContext()

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

pref = getSharedPreferences(String.valueOf(getApplicationContext().getPackageName()), 
Context.MODE_PRIVATE);
if (pref.getBoolean("isFirstTime", true)) {   // default true for first time
    editor = pref.edit();
    editor.putBoolean("isFirstTime", false).apply(); // update  so it will false ever after

    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)
                            .focusBorderSize(15)
                            .focusBorderColor(Color.parseColor("#FFA64D"))
                            .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();
                                        }
                                    });
                                }
                            }).closeOnTouch(false)
                            .build();
                    mFancyShowCaseView.show();
                }
            }, 50
    );
}
return super.onCreateOptionsMenu(menu);
}

您的案例值立即提交了 sharedpreferences,请参考此 link:what is the Best way to use shared preferences between activities