开发者

Android Save Webview to storage for recall later on reload of activity

I have an activity that loads a webview:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    webview = (WebView) findViewById(R.id.dashboard_webview);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setWebViewClient(new HelloWebViewClient());

    if (savedInstanceState != null) 
    {
        webview.restorePicture(getIntent().getExtras(), f);
        webview.restoreState(getIntent().getExtras());
    }
    else
    {
        webview.loadUrl("http://www.yahoo.com");
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) 
{
    super.onSaveInstanceState(outState);
    WebBackForwardList lList = webview.saveState(outState);
    boolean savedPic = webview.savePicture(outState, f);

I also have a "toolbar" at the bottom of the screen that launches another activity, which is essentially doing the same thing, loading a URL. The issue arises when I want to switch back to the first activity. When I leave the first activity the onSaveInstanceState is being called, but if I simply try to save to the bundle onCreate will still return a null bundle every time, not sure why.

So I think the best option is to simply save the webview somehow on my own. What's the best way to do this or alternativly why in the world would I never get a bundle that isn't null even though I save the bundle? Just for reference here is my toolbar which is included at the bottom of each layout:

package com.tseng.example;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Toolbar extends LinearLayout 
{

    public Toolbar(final Context context) 
    {
        super(context);
    }
    public Toolbar(final Context con, AttributeSet attrs) 
    {
        super(con, attrs);
        setGravity(Gravity.BOTTOM);
        // setMinimumHeight(50);

        setBackgroundColor(getResources().getColor(android.R.color.transparent));

        LayoutInflater inflater = (LayoutInflater)       con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.navigation, this);

        TypedArray a = con.obtainStyledAttributes(attrs, R.styleable.Toolbar);
        String option = a.getString(R.styleable.Toolbar_tab_id);

        String resourceId = "com.tseng.example:id/" + option;
        int optionId = getResources().getIdentifier(resourceId, null, null);
        TextView currentOption = (TextView) findViewById(optionId);
         currentOption.setBackgroundColor(getResources().getColor(android.R.color.white));
        currentOption.setTextColor(getResources().getColor(android.R.color.black));
        currentOption.requestFocus(optionId);
        currentOption.setFocusable(false);
        currentOption.setClickable(false);

        TextView tab1 = (TextView) findViewById(R.id.tab1);
        tab1.setOnClickListener(new OnClickListener() 
            {
            public void onClick(View v) 
                    {
                Intent intent = new Intent(con, ShowDashboard.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                //con.startActivity(intent);
                Activity lAct = ((Activity)con);
                //((Activity)con).finish();
                ((Activity)con).startActivity(intent);

            }
        });

        TextView tab2 = (TextView) findViewById(R.id.tab2);
        tab2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(con, ShowLog.class);
                //con.startActivity(intent);
                intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                Activity lAct = ((Activity)con);
                //((Activity)con).finish();
                ((Activity)con).startActivity(intent);开发者_开发问答
            }
        });

        TextView tab3 = (TextView) findViewById(R.id.tab3);
        tab3.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(con, ShowCalls.class);
                con.startActivity(intent);
            }
        });

        TextView tab4 = (TextView) findViewById(R.id.tab4);
        tab4.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(con, ShowTimer.class);
                con.startActivity(intent);
            }
        });

    }


}


I suspect that the problem may lie in your onCreate method. Add a call to super.onCreate() and see if that fixes the problem.

If that doesn't work, try using onRestoreInstanceState instead, see if that gives you a better result.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜