motoku
motoku

Reputation: 1581

Refreshing a crashed Android WebView

I have a WebView i swap with an EditText using setContentView. The EditText is used to modify Javascript source. The WebView uses loadView to render the Javascript source wrapped in HTML "script" tags. My problem is I am not able to refresh the WebView after i attempt to run bad Javascript code. I have tried the refresh() method, I have even tried instantiating a completely new WebView; so maybe I misunderstand the problem. How can my application recover from running a bad script?

I originally figured the source would not be necessary; but since it's short:

public class MainActivity extends Activity {
    boolean render = true;
    EditText text;
    WebView html;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        text = new EditText(this);
        text.setGravity(Gravity.TOP);
        text.setTypeface(Typeface.MONOSPACE);
        text.setBackgroundColor(Color.WHITE);
        text.setTextColor(Color.BLUE);
        text.setTextSize(14);
        html = new WebView(this);
        html.getSettings().setJavaScriptEnabled(true);
        load();
        //show();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowHomeEnabled(false);              
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setTitle("");
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.TabListener tabListener = new ActionBar.TabListener() {
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
                switch (tab.getPosition()) {
                    case 0:
                        render = true;
                        break;
                    case 1:
                        render = false;
                        break;
                }
                show();
            }

            public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
                // hide the given tab
            }

            public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
                if (tab.getPosition() == 0)
                    html.reload();
            }
        };
        actionBar.addTab(
            actionBar.newTab()
            .setText("render")
            .setTabListener(tabListener));
        actionBar.addTab(
            actionBar.newTab()
            .setText("source")
            .setTabListener(tabListener));
    }
    public void onBackPressed() {
        return;
    }
    void show() {
        runOnUiThread(new Runnable() {
            public void run() {
                if (!render) {
                    setContentView(text);
                } else {
                    String page = "<script language=\"javascript\">" + text.getText().toString() + "</script>" +
                        "<body bgcolor=\"#ffff00\" text=\"#444444\" />";
                    html.loadData(page, "text/html", Charset.defaultCharset().displayName());
                    setContentView(html);
                }
            }
        });
    }
    String filename = "hypertext.dat";
    void save() {
        try {
            FileOutputStream outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
            outputStream.write(text.getText().toString().getBytes());
            outputStream.close();
        } catch (Exception e) {
            // -
        }
    }
    void load() {
        try {
            FileInputStream inputStream = openFileInput(filename);
            BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
            final StringBuilder total = new StringBuilder();
            String line;
            while ((line = r.readLine()) != null) {
                total.append(line + "\n");
            }
            r.close();
            inputStream.close();
            runOnUiThread(new Runnable() {
                public void run() {
                    text.setText(total.toString());
                }
            });
        } catch (Exception e) {
            // -
        }
    }
    public void onPause() {
        save();
        super.onPause();
    }
    public void onDestroy() {
        save();
        super.onDestroy();
    }
    public void onRestart() {
        super.onRestart();
        load();
    }
}

Upvotes: 0

Views: 81

Answers (1)

motoku
motoku

Reputation: 1581

Adding System.exit(0); to void onDestroy() { } causes the WebView to sufficiently refresh();

Upvotes: 1

Related Questions