0

On my tab host is there anyway I can save a value from an edit text with the click of a button in Tab 5 and then use that value in tabs 1-4?

I have tried this, but I am pretty sure that this only saves it to tab 1.

public class Tab5 extends Activity{


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

    Button btnGo;
    btnGo = (Button) findViewById(R.id.button1);
    final EditText edit = (EditText) findViewById(R.id.editText1);

    btnGo.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) { 
            SharedPreferences myPref = getSharedPreferences("YourPrefrenceName",Context.MODE_PRIVATE);

            SharedPreferences.Editor editor = myPref.edit();
            editor.putString("YourStringKey",edit.getText().toString());
            editor.commit();
                        }

Tab 1

public class Tab1 extends Activity {

private WebView webView;

public final static String URL = "http://outputapps.com/build/infoview.php?id=";

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab1);

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    SharedPreferences myPref = getSharedPreferences("YourPrefrenceName",Context.MODE_PRIVATE);
    String mText =   myPref.getString("YourStringKey","");
    webView.loadUrl(Tab1.URL + mText);
    webView.setWebViewClient(new MyAppWebViewClient());
    WebSettings settings = webView.getSettings();
        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);

}
bumbob
  • 69
  • 7

3 Answers3

2

You can use SharedPrefrences to store primitive data values at any point:

For Storing:

SharedPrefrences myPref = getSharedPrefrences("YourPrefrenceName",Context.MODE_PRIVATE);

SharedPrefrences.Editor editor = myPref.edit();
editor.putString("YourStringKey",editText.getText().toString());
editor.commit();

For retrieval:

SharedPrefrences myPref = getSharedPrefrences("YourPrefrenceName",Context.MODE_PRIVATE);
String mText =   myPref.getString("YourStringKey","");

Now You can use mText wherever you want.

Pankaj
  • 2,115
  • 2
  • 19
  • 39
  • ok so I think that worked but i can't tell i tried saving it and when i press the button it should make the tabs refresh because the url changed right? or do i need more code to refresh the webview once it notices a new url, will edit question with the code – bumbob May 16 '14 at 20:12
  • Yes you will need more code to refresh your WebView. – Pankaj May 16 '14 at 20:15
  • do you have any idea where i would start? or maybe a link to a good reference, i've been searching but can't find anything – bumbob May 16 '14 at 20:17
  • You can use WebViewClient.shouldOverrideUrlLoading to track every url change to Webview Client – Pankaj May 16 '14 at 20:17
  • may be you can have a look at this answer at SO http://stackoverflow.com/questions/2563325/is-there-a-better-way-to-refresh-webview – Pankaj May 16 '14 at 20:18
  • not quite what i was looking for, when i press the button on tab 5 i want tabs 1-4 to reload with the new value in the url, so that a different portion of the site loads. – bumbob May 16 '14 at 20:26
  • then you may have to dig a bit more to achieve this – Pankaj May 16 '14 at 20:27
  • Just use .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) to your tab class to refresh each time you select your tab.and then on selection of particular tab you can reload your WebViewClient. – Pankaj May 16 '14 at 20:29
1

there are 3 ways for saving you can use :-

  1. Create a variable like public static String etxString=null; in a separate class something like Constants.java , now in the activity where you have the edittext (5th tab) you will intialize this like Constants.etxString=edit.getText().toString(); now if u want to use this in tab 1 -4 just use it like this try{Toast.makeText(this,Constants.etxString,Toast.LENGTH_SHORT).show();}catch(Exception e){e.printStackTrace();} noticed that i have surrounded with try catch because until 5th is not called this will throw nullpointer exception as the string was null.

Remember this will not guarantee the value presence if you exit the app ( as far as i know ) but this would be the fastest method.

  1. the second option is to use sharedpreference store and retrieve the String.

  2. would be DB.

  3. would be file .

These are all the options are available as far as i know with respect to storage....

As for comparing and refreshing you can have two strings either static or sharedpreference one will hold the present value or will always change outside the activity and one will be used to hold the previous url and this you will change in the webview activity and in onResume() of the activity containing the webview you will do something like if(!currentUrl.equals(dynamicUrl)) refreshWebView(); where refreshWebView() is a custom function where webview is reloaded...

Hope it helps thx.....

Ahmad
  • 437
  • 1
  • 4
  • 12
-1

You can make your EditText in Tab1 like:

  protected static EditText editText;

It will make it visable in other activities and then in you Tab make something liek this

  String text = Tab5.editText.getText().ToString(); 

and you can make this how you want and where you want

user3465277
  • 219
  • 3
  • 10
  • how would i implement that into my tab 5 where the edit text is, see question for edit in code, it now displays all of tab 5 – bumbob May 16 '14 at 20:04
  • in Tab5 can be Tab1,Tab2,Tab3 first line of code must be in activity where you store data and second line in whatever activity you want to use this – user3465277 May 16 '14 at 20:18