0

I have made a [very] simple app for android which connects to a URL saved in the preferences. I have been able to get the app to open a new activity / layout to enter the URL and save it, then I use finish(); to go back to the main screen (WebView).

How do I get the WebView to refresh after setting the new URL?

[EDIT]

I don't think my preferences are being saved:

mySettings.java

package com.rarecreativegroup.dcm1config;

import android.app.Activity;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
public class mySettings extends Activity {
    public void onCreate (Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.menu_layout); 




        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {


            public void onClick(View v) {

                Editor editor = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();
                String string = getString(R.string.ipaddress);
                editor.putString("ipaddress", string);              
                editor.commit();
                WebView webViewer = (WebView)findViewById(R.id.webview);
                //webViewer.loadUrl(string);
                finish();
            }
        });
    }

    }

mainActivity.java

package com.rarecreativegroup.dcm1config;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebView;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {
    public static final String PREFS_NAME = "preferences";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        String uri = settings.getString("ipaddress", "");
        WebView webview = new WebView(this);
        webview.loadUrl(uri);
        setContentView(webview);
        //setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()) {
            case R.id.settings:
                startActivity(new Intent(this, mySettings.class));
                return true;
            case R.id.refresh:
                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                String uri = settings.getString("ipaddress", "");
                WebView webview = new WebView(this);
                webview.loadUrl(uri);
                setContentView(webview);
            default:
                return super.onOptionsItemSelected(item);
        }

    }


    }

res/xml/preferences.xml

 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        <PreferenceCategory 
        android:title="IP Settings"
        android:key="ip_settings">

        <EditTextPreference
            android:key="ipaddress"
            android:title="DCM1 IP" 
            android:summary="Define the IP address of the DCM1"
            android:dialogTitle="DCM1 IP Address"
            android:dialogMessage="Supply IP Address"    
            android:defaultValue="http://192.168.6.117/" />

    </PreferenceCategory>
    </PreferenceScreen>

Any ideas?

2 Answers2

0

You can get a reference of your web view only inside your Activity class, or having a reference of your activity, doing this:

WebView webView = (WebView)findViewById(R.id.my_web_view);

And then, to reload your web view:

webView.loadUrl("http://www.websitehere.php");

or more simply:

webView.reload();
Stefano Ortisi
  • 5,288
  • 3
  • 33
  • 41
0

The problem you are having is your are referencing two different SharedPreferences. You are storing it in the default shared prefs (which is good) in the settings activity but then retrieving it from your own custom one. Just call the same default shared prefs both times. Here is what you need to do:

Change

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

in your mainActivity to:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

and Change:

Editor editor = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();

in your mySettings activity to:

Editor editor = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit();

And everything should start to work fine.

Salil Pandit
  • 1,498
  • 10
  • 13
  • Thank you so much. I am still having problems, I have replaced the code with your suggested alterations, but I can seem to get it to work. Let me know if you need anything adding above for you to help me – user1474819 Jul 13 '12 at 20:45
  • Have you put in log statements to see what uri its retrieving? Also, you should need to create a new web view and reset it as contentView. Instead keep a class-level reference to the webview you create in the onCreate and just called `mWebView.loadUrl(uri);` – Salil Pandit Jul 13 '12 at 21:17
  • Also, you know you are pulling the ipaddress from your strings.xml file and not from the EditTextPreference, right? The R.string.ipaddress is an id in your strings.xml file in your res/values/ folder... – Salil Pandit Jul 13 '12 at 21:20