为WebView设置新的上下文 [英] set a new context to WebView

查看:252
本文介绍了为WebView设置新的上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以更改WebView的上下文? 我的意思是,当我扩展WebView类时,我用上下文(在我的示例中是一个活动)调用超级构造函数.稍后,此活动不再存在,我们转到其他活动.

Is there any way to change the context of WebView? I mean, when I extend the WebView class I call the super constructor with a context (in my example an activity). Later on, this activity is no longer exists and we move to other activity.

问题在于WebView仍然存在,并且具有对此上下文的引用. 因为初始化时间太长,所以我无法随时创建WebView. 我试图用getApplicationContext()调用超类,但发现这对于显示视频也有问题.

The problem is the WebView is still alive and has a reference to this context. I can't create my WebView any time because the initialisation is long. I tried to call the super class with getApplicationContext() but I found that this also problematic for displaying videos.

我该怎么做才能设置WebView的上下文而不创建新的上下文.

What can I do in order to set the WebView's context without create a new one.

谢谢.

推荐答案

要将新的上下文传递到webview,您可以创建一种方法来初始化webview,并传递Context的参数,如下所示:

To pass new context to webview you can create a method to initialize webview, passing an argument of Context like shown below:

public static Webview initializeWebView(Context context)
{

  myWebView = new WebView();
  return myWebView;

}

然后,您可以在任意位置和任意位置调用此方法.您可以按如下所示调用它:

And after this you can call this method whereever you want and whenever you want. You can call this as shown below:

myWebView = initializeWebView(YourActivityName.this);
//this way whatever Context you will pass your webview will be initialized that way
//for example you can also pass getApplicationContext() as an Argument
myWebView = initializeWebView(getApplicationContext());
//or
myWebView = initializeWebView(customContext);

此customContext可以是从您要使用的其他上下文继承的任何上下文.

this customContext can be any context that is inherited from other context that you wanted to use.

这篇关于为WebView设置新的上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆