This question already has an answer here:
-
How do I pass data between Activities in Android application?
52 answers
I have two Activity, One is Mainactivity and another is Secondactivity. Secondactivity contains Webview that loads local HTML pages from assets folder.
Mainactivity contains buttons labeled as Button A and Button B when pressed would start Secondactivity. I would like to pass the string as URL from Mainactivity to Secondactivity which loads the A.html and B.html when Button A and Button B is pressed.
For now, I have following code in Mainactivity Class
Fragment firstFragment1 = new browser();
Bundle args1 = new Bundle();
args1.putString("url1", "file:///android_asset/diploma.html");
firstFragment1.setArguments(args1);
moveToFragment(firstFragment1);
break;
and on SecondActivity Class, I have following code
String url1 = getArguments().getString("url1");
myWebView=(WebView)rootView.findViewById(R.id.webview);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.getSettings().setLoadsImagesAutomatically(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.setInitialScale(1);
myWebView.getSettings().setLoadWithOverviewMode(true);
myWebView.getSettings().setUseWideViewPort(true);
WebSettings webSettings = myWebView.getSettings();
myWebView.loadUrl(url1);
return rootView;
}
Which work for Fragment flawlessly, but how do I make it work for Activity to activity??
In first activity you should put extra argument to intent like this:
// I assume Web.class is your second activity
Intent intent = new Intent(this, Web.class);
intent.putExtra("url", your_url);
startActivity(intent);
Then in second activity you retrive argument like this:
String url = getIntent().getExtras().getString("url");
webView.load(url);
If you're using activities, pass the url as a string and get it in the next activity.Like below code
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("url","somepage.com");
startActivity(intent);
In the webView,get it like this
String url = getIntent().getStringExtras("url");
webView.load("url");
Make one single common activity which load web url
for example:
public class WebviewActivity extends AppCompatActivity {
Activity mActivity;
WebView webview_;
String title ="";
String url ="";
ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help_webview_activity);
mActivity = this;
findviews();
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
title = bundle.getString("title");
url = bundle.getString("Url");
setAction();
}
else {
webview_.setVisibility(View.GONE);
}
}
private void setAction() {
try {
//check internet connection first
if (CommonUtils.isConnectingToInternet(WebviewActivity.this)) {
webview_.setVisibility(View.VISIBLE);
WebSettings webSettings = webview_.getSettings();
webview_.setWebViewClient(new MyWebViewClient());
webSettings.setJavaScriptEnabled(true);
webview_.getSettings().setSupportZoom(true);
webview_.getSettings().setBuiltInZoomControls(true);
webview_.getSettings().setDisplayZoomControls(true);
webview_.loadUrl(url);
}
else {
//alert - no internet connection
}
} catch (Exception e) {
//print exp
e.printStackTrace();
}
}
private void findviews() {
webview_ = (WebView) findViewById(R.id.wbview);
}
public class MyWebViewClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(dialog == null){
dialog = ProgressDialog.show(WebviewActivity.this, null, "Loading...");
dialog.setCancelable(true);
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(dialog.isShowing())
dialog.dismiss();
}
}
}
Xml code :
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_login_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="ltr"
tools:context=".activity.WebviewActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:supportsRtl="false"
app:popupTheme="@style/AppTheme.PopupOverlay">
<include layout="@layout/header_settinglayout" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/wbview"
>
</WebView>
</RelativeLayout>
Now you can load any url or html from whole app
From Activity :
if (CommonUtils.isConnectingToInternet(HelpActivity.this)) {
Intent i = new Intent(HelpActivity.this,WebviewActivity.class);
i.putExtra("title",getResources().getString(R.string.faqs));
i.putExtra("Url", "https://stackoverflow.com/questions/48594734/how-to-pass-string-url-from-main-activity-to-next-activity-and-load-url-in-webvi");
startActivity(i);
}
else {
//alert - no internet
}
From Fragment :
if (CommonUtils.isConnectingToInternet(getActivity())) {
Intent i = new Intent(getActivity(),WebviewActivity.class);
i.putExtra("title",getResources().getString(R.string.faqs));
i.putExtra("Url", "https://stackoverflow.com/questions/48594734/how-to-pass-string-url-from-main-activity-to-next-activity-and-load-url-in-webvi");
getActivity().startActivity(i);
}
else {
//alert - no internet
}
Webview load url
i.putExtra("Url", "https://stackoverflow.com/questions/48594734/how-to-pass-string-url-from-main-activity-to-next-activity-and-load-url-in-webvi");
Webview load html from assets directory
i.putExtra("Url", "file:///android_asset/diploma.html");