0

I am trying to implement a SharedPreferences. I already search it, try it, for almost 12 hours and its still cannot help my problem.

Java Code

public static final String MyPREFERENCES = "banner_pref" ;
SharedPreferences SharedP;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    SharedP = getSharedPreferences("banner_pref", MainHelloballi.MODE_PRIVATE);
    SharedPreferences.Editor editor = SharedP.edit();
    editor.putBoolean("banner_pref", true);
    editor.commit();
    Boolean myValue = SharedP.getBoolean("banner_pref", true);

    if (myValue == true){
        bannerfull = (RelativeLayout) findViewById(R.id.banner_full);
        bannerfull.setVisibility(View.VISIBLE);

        editor = SharedP.edit();
        editor.putBoolean("banner_pref", false);
        editor.commit();

    }

    else {

        bannerfull = (RelativeLayout) findViewById(R.id.banner_full);
        bannerfull.setVisibility(View.GONE);


        //SharedP.edit().putBoolean("banner_pref", false).commit();

    }
}

I know that my SharedPreferences value is keep True. So my question is, how to make it not true when I open the same Activity?

I really appriciate it if anyone can give me a sample from my code. So I can learn from it .

Thanks a lot before.

Matthew
  • 95
  • 4
  • 14
  • if it is `isItFirstTime` you are having troubles with (will always be true), put it inside an if statement that First checks it isnt already set. Then set to true, else false – IAmGroot Jul 29 '14 at 17:12
  • Chnge this line: Boolean myValue = SharedP.getBoolean("banner_pref", true); to this Boolean myValue = SharedP.getBoolean("isItFirstTime", true); – user1888162 Jul 29 '14 at 17:12
  • @user1888162 sorry, i put it wrong. I edit the code – Matthew Jul 29 '14 at 17:14
  • @Doomsknight can you gave me an example? Sorry for asking too much – Matthew Jul 29 '14 at 17:19
  • http://stackoverflow.com/questions/5950043/how-to-use-getsharedpreferences-in-android – user1888162 Jul 29 '14 at 17:23
  • @user1888162 in my case, I use the `SharedPreferences` to show the `WebView` just once everytime I open the application. So when I open another activity, and back again it won't show. But when I re start the application, It'll show again. – Matthew Jul 29 '14 at 17:26
  • `editor.putBoolean("banner_pref", true);` you are explicitly setting it to true. what else would you expect to happen? – njzk2 Jul 29 '14 at 17:29
  • On Android, the concept of "opening an application" vs a given activity is somewhat foreign, and so a bit tricky to *define* precisely enough to implement in code. – Chris Stratton Jul 29 '14 at 17:46

2 Answers2

0

I would throw your statement:

editor.putBoolean("banner_pref", true);

into your else statement. This will prevent it from being set before it can be changed if your if is hit.

Ryan Sayles
  • 3,389
  • 11
  • 56
  • 79
  • Hi, thank. But when I go back to the activity for the third time, it will show the `webview` again – Matthew Jul 29 '14 at 17:43
  • What you can do as a work around is have a `SharedPreference` to store as another boolean to say if your boolean has been set or not, that way you can just do something like `if(oldBooleanHasBeenSet){//do something} else{ //do something else}` I have done this before and it works pretty well, just set the new boolean to true after your first `editor.commit()` – Ryan Sayles Jul 29 '14 at 17:47
0

I just made a little fix to your code, and it should work like this

* Read the value with default is `true`, if the `SharedPreferences` doesn't exist before, certainly, it is the first time; `true` value is certain.
* if value is `true`, it is time to show the view; and then, update the value to `false` and save to `SharedPreferences`.
* else if value is `false`, hide the view.
* next time, when the `Activity` is launched, the value will be read as `false` (assuming there is none intervention in between, for example, users clear data from `Settings->Apps`...); the view is hidden, assuming there isn't any other action that update value to `true`.

Here the code:

public static final String MyPREFERENCES = "banner_pref" ;
SharedPreferences SharedP;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // load sharedpref value 
    SharedP = getSharedPreferences("banner_pref", MainHelloballi.MODE_PRIVATE);
    Boolean myValue = SharedP.getBoolean("banner_pref", true); // default: true

    // load layout, we just need it anyway
    bannerfull = (RelativeLayout) findViewById(R.id.banner_full);

    // if true, show it
    if (myValue == true) {
        bannerfull.setVisibility(View.VISIBLE);
        // ok, now switch value to 'false' and commit to SharedP
        editor = SharedP.edit();
        editor.putBoolean("banner_pref", false);
        editor.commit();
    }
    // if false, hide it
    else {
        bannerfull.setVisibility(View.GONE);
    }
}

Update: display every three times it launches. By adding a new value to counter number of display.

public static final String MyPREFERENCES = "banner_pref" ;
SharedPreferences SharedP;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // load sharedpref value 
    SharedP = getSharedPreferences("banner_pref", MainHelloballi.MODE_PRIVATE);
    Boolean myValue = SharedP.getBoolean("banner_pref", true); // default: true
    int counter = SharedP.getInt("counter", 1); // default starting from 1

    if(counter == 3) {
      myValue = true;
      // counter reset
      counter = 0;
    }

    // load layout, we just need it anyway
    bannerfull = (RelativeLayout) findViewById(R.id.banner_full);

    // if true, show it
    if (myValue == true) {
        bannerfull.setVisibility(View.VISIBLE);
        // ok, now switch value to 'false' and commit to SharedP
        editor = SharedP.edit();
        editor.putBoolean("banner_pref", false);
        editor.commit();
    }
    // if false, hide it
    else {
        bannerfull.setVisibility(View.GONE);
    }

    // each time loading, increase the counter
    ++counter;
    editor = SharedP.edit();
    editor.putInt("counter",  counter);
    editor.commit();
}
Pete Houston
  • 14,931
  • 6
  • 47
  • 60
  • What If, everytime I open the application the view will show again? – Matthew Jul 29 '14 at 17:53
  • eh? just remove all part relating to `SharedPreferences`; the view will show all the time. – Pete Houston Jul 29 '14 at 17:55
  • No, I mean is it will show again if the user restart the application. But, if the user just playing around in application it won't show – Matthew Jul 29 '14 at 17:57
  • eh? you're kidding...first, `it shows in app restart`; second, `user playing around in application it won't show` - is the logic correct? – Pete Houston Jul 29 '14 at 18:01
  • Wait, let me explain it. So when the user open the application for the first time, the webview (`its like a ads in fullscreen`) will show. After the user press the close button, it won't show again until the user close the application and open again. – Matthew Jul 29 '14 at 18:04
  • Then just do it normally, it's not matter of first time or second time, it is every time the app is launched, and the ad should be opened first. The close button is to close the ad; the rest of app runs normally. What you need to do is: `onCreate` - show the ad, add a `View.OnClickListener` to the close button, with `onClick()` action is to hide the ad. That's all you need to do. – Pete Houston Jul 29 '14 at 18:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/58277/discussion-between-matthew-and-xjaphx). – Matthew Jul 29 '14 at 18:09