【问题标题】:Android Save Webview to storage for recall later on reload of activityAndroid 将 Webview 保存到存储中,以便稍后在重新加载活动时调用
【发布时间】:2011-03-18 22:49:45
【问题描述】:

我有一个加载 web 视图的活动:

@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);

我在屏幕底部还有一个“工具栏”,它启动另一个活动,它本质上是做同样的事情,加载一个 URL。当我想切换回第一个活动时,就会出现问题。当我离开第一个活动时,正在调用 onSaveInstanceState,但如果我只是尝试保存到包,onCreate 仍然每次都会返回一个空包,不知道为什么。

所以我认为最好的选择是我自己以某种方式简单地保存 webview。做到这一点的最佳方法是什么,或者为什么即使我保存了捆绑包,我也永远不会得到一个不为空的捆绑包?仅供参考,我的工具栏包含在每个布局的底部:

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);
            }
        });

    }


}

【问题讨论】:

    标签: android android-activity webview


    【解决方案1】:

    我怀疑问题可能出在您的 onCreate 方法上。添加对 super.onCreate() 的调用,看看是否能解决问题。

    如果这不起作用,请尝试改用onRestoreInstanceState,看看这是否会给您带来更好的结果。

    【讨论】:

      猜你喜欢
      • 2022-10-14
      • 2014-06-03
      • 2021-12-02
      • 2016-09-04
      • 1970-01-01
      • 2022-10-05
      • 2012-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多