0

I have a WebView that will show in fullscreen when I run the Activity.

I want that WebView only open just once everytime I open the application.

So if I close the application, then I start again it will show the WebView. But when i close the webView, go to another activity, and then back to the activity that have a same Webview ,the WebView won't show again.Unless until I restart the application.

How to do something like that? I already search it and its looks like I can use the SharedPreferences, but most of them used it for a single activity. Can I use it for a single WebView?

Matthew
  • 95
  • 4
  • 14
  • Sure. You have to put the logic into the hosting fragment/activity and check if the application has shown the webview. At app start reset the property. – schlingel Jul 29 '14 at 12:20
  • @schlingel How to reset the property? Maybe a clue how to do it. – Matthew Jul 29 '14 at 12:22

1 Answers1

0

You have to store some value in some variable of sharedpreference. When user open your app,a check of the value of that variable every time will happen by retrieving it from sharedpreference . Let the variable be boolean, and the value retrieved is true,then edit this value and change it to false.While navigating through your app,nothing will hapeen to this variable's value.Even if the first activity on which you displayed a webview is reopened in someway because the variable needs to be true to get your webview code executed (which you can accomplish by loading webview in an if statement checking your variable's value),but when oncreate is called and it will check for that variable it will find t as false. When ever user closes your application then again change that variable to true in the sharedpreferences .And on next login to your app,the webview will be loaded

 SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();
    editor.putBoolean("isItFirstTime", true);
    editor.commit();

the you can get it as:

 SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
 Boolean myValue = sp.getBoolean("isFirstTime", false);

also see this How to use SharedPreferences in Android to store, fetch and edit values

Community
  • 1
  • 1
nobalG
  • 4,544
  • 3
  • 34
  • 72