0

I am doing hybrid application. It has two activities login and home.

On login activity a web view with many java scripts is loaded. Now I want to use same webview in home activity because it takes some time to load all JavaScript’s.

As per android guide lines we cannot pass view from one activity to another.

How to fix this?

R World
  • 766
  • 9
  • 26
  • 1
    Get your Web content to load faster. Or, do not have separate activities for "login and home". Implement that in your HTML in one activity. – CommonsWare May 14 '13 at 13:41
  • well thanks for quick reply. can this possible with use of fragments? like one fragment contains this webview and we will just ditach/attach it – R World May 14 '13 at 13:57
  • You cannot pass fragments between activities, either. – CommonsWare May 14 '13 at 14:00
  • I mean with addView /RemoveView in any activity lifecycle with inheritance... – R World May 14 '13 at 14:06
  • Inheritance does not matter. I know of no way to safely pass widgets or fragments between activities. For example, `WebViewFragment`, explicitly destroys and recreates its `WebView` on a configuration change, and that's even where the same activity *class* is used for both the old and new activity instances. Yet another solution for your problem is to make one or the other activity not be hybrid, but rather use native Java. – CommonsWare May 14 '13 at 14:08

1 Answers1

1

I'll reference from @rupps's answer here

MutableContextWrapper mMutableContext=new MutableContextWrapper(context);
WebView mWebView=new WebView(mMutableContext);

and when you want to change the parent of this webview, you can change the context of it.

mMutableContext.setBaseContext(newcontext);

I was also looking for a solution, and after a lot of overflowing my stack came to this solution. Thanks @rupps

Ahsan Zaheer
  • 646
  • 1
  • 12
  • 18