Android - Sharedpreferences

Asked

Viewed 77 times

0

It’s not working my sharedPreferences.

public static final String PREF_NAME = "TelaApresentacaoActivityPreferences";

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

       mPager = (ViewPager) findViewById(R.id.pager);
       mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
       mPager.setAdapter(mPagerAdapter);
       mPager.setOnPageChangeListener(newViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            invalidateOptionsMenu();
        }
    });

    sharedPreferences = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
}

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

    sharedPreferences = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.commit();

}

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

    sharedPreferences = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.clear();
    editor.commit();

}

On my Androidmanifest.xml, I put chunks of code inside

 android:onHistory="true"
  • 1

    What’s not working out?

  • when I close application and open it again it appears to Activityapresentacao.

  • Explain what you’re trying to do.

  • In my app I have a Screen Slide, I want to run only once, when user will close the application it is not visible to user again, as well as login too.

1 answer

0


Every time Activity is destroyed, data is removed:

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

    sharedPreferences = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.clear();
    editor.commit();

}

comment on this part :

 editor.clear();

To check if your app has already run, do the following:

           protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_screen_slide);

               mPager = (ViewPager) findViewById(R.id.pager);
               mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
               mPager.setAdapter(mPagerAdapter);
               mPager.setOnPageChangeListener(newViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    invalidateOptionsMenu();
                }
            });

            sharedPreferences = getSharedPreferences(PREF_NAME, MODE_PRIVATE);

boolean jaRodou = sharedPreferences.getBoolean("JaRodou", false);

if(jaRodou){
// se entrar aqui é pq já executou o codigo algiuma vez
}else{

            SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("JaRodou", true);
    editor.commit();
}
        }
  • didn’t work out, my Activity is still visible.

  • You need to save something.. I will update the answer

  • still remains visible, however he entered if, in Is he did not enter

  • then it’s true, right? there you do what you have to do, in your case hide your screen!

  • tendi. try here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.