0

To open activity from the main is,

Intent webviewIntent = new Intent(context, WebViewActivity.class);
webviewIntent.putExtra(WebViewActivity.URL_FROM_LINK, mUrlSpan.getURL());
startActivity(webviewIntent);

from here it open the activity,

public class WebViewActivity extends Activity {

public static final String URL_FROM_LINK = "UrlFromLinkClick";
public static final String BUNDLE_URL = "UrlFromBundle";
private String mNewsItemUrl;
boolean detailPage = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview);

    if (getIntent().getExtras() != null && getIntent().getExtras().containsKey(URL_FROM_LINK)) {
        mNewsItemUrl = getIntent().getExtras().getString(URL_FROM_LINK);
        Bundle bundle = new Bundle();
        bundle.putString(BUNDLE_URL, mNewsItemUrl);


        if (findViewById(R.id.displayDetail) != null) {
            detailPage = true;
            //getFragmentManager().popBackStack();

            WebViewFragment webViewFragment = (WebViewFragment) getFragmentManager().findFragmentById(R.id.displayDetail);
            if (webViewFragment == null) {
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                webViewFragment = new WebViewFragment();
                webViewFragment.setArguments(bundle);
                ft.replace(R.id.displayDetail, webViewFragment, "WebViewFragment1");
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
            }
        }

    }
}
}

And the fragment,

public class WebViewFragment extends Fragment {

 String mURL = "";
 String url;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  Log.v("DetailFragment", "onCreate()");
 }

 @Override
 public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);
  Log.v("DetailFragment", "onActivityCreated()");
  if (savedInstanceState != null) {
   mURL = savedInstanceState.getString("currentURL", "");
  }
  if(!mURL.trim().equalsIgnoreCase("")){
   WebView myWebView = (WebView) getView().findViewById(R.id.wvWebView);
   myWebView.getSettings().setJavaScriptEnabled(true);
   myWebView.setWebViewClient(new MyWebViewClient());
   myWebView.loadUrl(mURL.trim());
  }
 }

 @Override
 public void onSaveInstanceState(Bundle outState) {
  super.onSaveInstanceState(outState);
  outState.putString("currentURL", mURL);
 }

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  Log.v("DetailFragment", "onCreateView()");
  url = getArguments().getString(WebViewActivity.BUNDLE_URL);
  View view = inflater.inflate(R.layout.frag_webview, container, false);
  return view;
 }

 public void setURLContent(String URL) {
  mURL = URL;
 }

 public void updateURLContent(String URL) {
  mURL = URL;
  WebView myWebView = (WebView) getView().findViewById(R.id.wvWebView);
  myWebView.getSettings().setJavaScriptEnabled(true);
  myWebView.setWebViewClient(new MyWebViewClient());
  myWebView.loadUrl(mURL.trim());
 }

 private class MyWebViewClient extends WebViewClient {
     @Override
     public boolean shouldOverrideUrlLoading(WebView view, String url) {
             return false;
     }
 }
}

I have it open the activity from main by putextras, then is passed to the fragment by a bundle.putstring(). Im just confused on where is it supposed to load the url. Thanks ahead of time!

4

1 回答 1

1

代码看起来不错。你必须移动这条线

getArguments().getString(WebViewActivity.BUNDLE_URL);

inside onActivityCreated,因为您将要加载的 url 放在Fragment's 包中,并将其传递给myWebView.loadUrl

于 2013-07-05T16:56:06.323 回答