打开从活动传递到片段的特定URL

时间:2013-07-05 16:47:37

标签: android android-activity android-webview fragment

从主要打开活动是,

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

从这里打开活动,

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();
            }
        }

    }
}
}

片段,

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;
     }
 }
}

我让它通过putextras从main打开活动,然后通过bundle.putstring()传递给片段。我只是混淆它应该加载网址在哪里。提前谢谢!

1 个答案:

答案 0 :(得分:1)

代码看起来不错。你必须移动这一行

getArguments().getString(WebViewActivity.BUNDLE_URL);

onActivityCreated内,因为您将网址加载到Fragment的包内,并将其传递给myWebView.loadUrl