I have some problem with passing a WebView from the first activity to the second ac开发者_运维知识库tivity. I have tried putExtra
and so on, but it complains that I'm trying to pass a WebView instead of a String.
Intent intent = new Intent().setClass(this, WebView.class);
intent.putExtra("myPassedWebView", mWebView);
startActivity(intent);
Any ideas?
Thanks in advance!
Never pass widgets between activities. You will create memory leaks. Please find another solution to whatever problem you are trying to solve.
Very old topic but I had the same problem.
Not sure if this is a correct way to do it. But I solved the problem by creating a singleton class where I store the webview I use.
When I launch the new activity where I need the same webview I remove it from the parentLayout and add it again in the new activityLayout in onCreate();
There won't be memoryleaks since putExtra method is not used and you will only have one instance of the webview.
Create webview and store it in singletonclass:
Singleton.getInstance().setWebView((WebView) findViewById(R.id.mainwebview));
Remove the webview from parentLayout before you start activity:
((ViewGroup)Singleton.getInstance().getWebView().getParent()).removeView(Singleton.getInstance().getWebView());
Intent i = new Intent(getApplication().getBaseContext(), WebActivity.class);
startActivity(i);
in new activity onCreate() add webview in the new layout:
((LinearLayout)findViewById(R.id.webActivity)).addView(Singleton.getInstance().getWebView());
Then you can pass the webview between your activities the way you want.
精彩评论