マイSharedPreferences値は、何らかの理由で渡していません

スティーブンRõgrê:

だから私は、三つのクラスを作成しました:

1)スプラッシュ画面クラス2)イントロクラス3)主な活動クラス

アプリが最初に起動された場合は私の意図は、スプラッシュ画面の後に、罰金を作品イントロ活性を示すことが想定されています。

イントロの活動では、私はまた、罰金の作品、それがチェックされている場合にのみ、アプリを起動します契約条件のチェックボックスを追加しました。

問題は、スプラッシュ画面である - それは初めてであるか、それが初めてではない場合、ユーザが条件に同意した場合との両方である場合、それがチェックする場合は、アプリが起動されるたびに、私のアプリをチェックします渡された、主な活動を開始します。

私はこのためにSharedPreferencesを使用していますが、私はそれが期待どおりに動作していないので、あなたの助けが必要!

ここでのコードだSplashActivityは、

public class SplashActivity extends AppCompatActivity {

    private static final int SPLASH_DELAY = 200;

    private final Handler mHandler   = new Handler();
    private final Launcher mLauncher = new Launcher();

    @Override
    protected void onStart() {
        super.onStart();

        mHandler.postDelayed(mLauncher, SPLASH_DELAY);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.activity_splash);
    }

    @Override
    protected void onStop() {
        mHandler.removeCallbacks(mLauncher);
        super.onStop();
    }

    private void launch() {
        if (!isFinishing()) {

            SharedPreferences settings=getSharedPreferences("prefs",0);
            boolean firstRun=settings.getBoolean("firstRun",false);

            if(!firstRun)
            {
                SharedPreferences.Editor editor=settings.edit();
                editor.putBoolean("firstRun",true).apply();
                editor.commit();
                Intent i=new Intent(SplashActivity.this, IntroActivity.class);
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                startActivity(i);
                finish();
            }
            else
            {
                SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
                String name=preferences.getString("Policy","Disagreed");
                if(!name.equals("Agreed"))
                {
                    Intent a=new Intent(SplashActivity.this, MainActivity.class);
                    overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                    startActivity(a);
                    finish();
                } else
                {
                    Intent i=new Intent(SplashActivity.this, IntroActivity.class);
                    overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                    startActivity(i);
                    finish();
                }

            }
        }

    }

    private class Launcher implements Runnable {
        @Override
        public void run() {
            launch();
        }
    }
}

IntroActivity:

public class IntroActivity extends MaterialIntroActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        enableLastSlideAlphaExitTransition(true);

        getBackButtonTranslationWrapper()
                .setEnterTranslation(new IViewTranslation() {
                    @Override
                    public void translate(View view, @FloatRange(from = 0, to = 1.0) float percentage) {
                        view.setAlpha(percentage);
                    }
                });

        addSlide(new SlideFragmentBuilder()
                        .backgroundColor(R.color.myWindowBackground)
                        .buttonsColor(R.color.myAccentColor)
                        .image(R.mipmap.ic_splash_screen)
                        .title("Organize your time with us")
                        .description("Would you try?")
                        .build(),
                new MessageButtonBehaviour(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showMessage("We provide solutions to make you love your work");
                    }
                }, "Work with love"));

        addSlide(new SlideFragmentBuilder()
                .backgroundColor(R.color.myWindowBackground)
                .buttonsColor(R.color.myAccentColor)
                .title("Want more?")
                .description("Go on")
                .build());

        addSlide(new CustomSlide());

    }

    @Override
    public void onFinish() {
        Intent a=new Intent(IntroActivity.this, MainActivity.class);
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        startActivity(a);
        finish();
    }
}

CustomSlideフラグメント(個人情報保護方針チェックボックスフラグメント)

public class CustomSlide extends SlideFragment {
    private CheckBox checkBox;
    private SharedPreferences prefs;
    private SharedPreferences.Editor editor;
    private String terms;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_custom_slide, container, false);

        prefs = getActivity().getPreferences(Context.MODE_PRIVATE);
        editor = prefs.edit();
        terms = prefs.getString("Policy", "Disagreed");

        checkBox = (CheckBox) view.findViewById(R.id.checkBox);
        return view;
    }

    @Override
    public int backgroundColor() {
        return R.color.myWindowBackground;
    }

    @Override
    public int buttonsColor() {
        return R.color.myAccentColor;
    }

    @Override
    public boolean canMoveFurther() {

        editor.putString("Policy", "Agreed");
        editor.commit();

        return checkBox.isChecked();
    }

    @Override
    public String cantMoveFurtherErrorMessage() {

        editor.putString("Policy", "Disagreed");
        editor.commit();

        return getString(R.string.error);
    }
}

MainActivityは渡さ何の関係もありませんし、ちょうど開始します。

いくつかの助けのために楽しみにして、ありがとうございます。

編集:私のアプリのショーIntroSlider初めてのI打ち上げ、それは直接ボタンを同意したの目的に反し主な活性を示す個人情報保護方針(CustomSlide断片)のチェックボックスを「同意」し、再起動チェックせずに、それを閉じます。ユーザーが同意するボタンをクリックした後、アプリのよう本体のみ活性を示す必要があります。

Abdomns:

:簡単にコード管理のために私は、ユーザーが合意した場合/反対し、チェックのためにSharedPreferencesに対してget /プット・ブールのための2の静的メソッドを作成します。

プットブールのために:

public static void Put_boolean(String key, boolean value, Context context) {
    SharedPreferences sharedPreferences =  context.getSharedPreferences("prefname", 0);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean(key, value);
    editor.apply();
}

GETブールのために:

public static boolean Get_boolean(String key, boolean defvak, Context context) {
    SharedPreferences sharedPreferences =  context.getSharedPreferences("prefname", 0);
    return sharedPreferences.getBoolean(key, defvak);
}

ユーザーが合意しただけで、今ユーザーが合意されたとき、我々は真の値を保存することができますし、falseの場合、ユーザー反対し、/反対する呼び出し:

YourActivity.Put_boolean("isAgreed", true, context); //when Agreed
YourActivity.Put_boolean("isAgreed", false, context); //when Disagreed

ユーザーが合意した場合/反対するの使用を確認したいとき:

final boolean checkifAgreed = YourActivity.Get_boolean("isAgreed", false, context);
if(checkifAgreed){
            //user is Agreed
        }else{
            //user is Disagreed
        }

私は、同じ方法を使用して、任意のアクティビティから/入れSharedPreferences値を取得することができますので、このヘルプあなたを願って、静的メソッドを使用しました

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=234550&siteId=1