如何将字符串数据从Main-activity发送到片段?

时间:2017-05-04 11:41:34

标签: android android-fragments webview

我在片段中有一个Web视图。我想通过从主要活动中选择一个网址来加载webview。例如,我的webview正在加载网址http://google.com/,该网址来自(url)我的主要活动。换句话说,我的主要活动中有一个类似下面的字符串,我想用它来加载片段中的网页视图,我该怎么做? 请帮我解决这个问题

String url="http://google.com/";

5 个答案:

答案 0 :(得分:2)

使用此功能将数据发送到活动中的片段

Bundle bundle=new Bundle();
bundle.putString("url", "http://google.com/");
  //set Fragmentclass Arguments
Fragmentclass fragobj=new Fragmentclass();
fragobj.setArguments(bundle);

然后接受片段的onCreateView方法

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
          String strtext=getArguments().getString("url");

    return inflater.inflate(R.layout.fragment, container, false);
    }

答案 1 :(得分:1)

试试这个:

Fragment homeFragment = new LotteryDetailFragment();
            FragmentTransaction homeTransaction = getFragmentManager().beginTransaction();

            String str = "you string";
            Bundle bundle = new Bundle();
            bundle.putString("param_str", str);
            homeFragment.setArguments(bundle);
            homeTransaction.addToBackStack("HomeFragment");
                     homeTransaction.replace(R.id.frame_container, homeFragment, "HomeFragment");
            homeTransaction.commit();

在目标片段的onCreateView上

  String str=getArguments().getString("param_str");

答案 2 :(得分:1)

试试这个:

1 - 来自活动:

    Bundle bundle = new Bundle();
    bundle.putString("url", "www.google.com");

    Fragment fragment = new YourFragment();
    fragment.setArguments(bundle);
    getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();

2 - 在片段中:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View layoutView = inflater.inflate(R.layout.your_layout, container, false);
    this.activity = getActivity();
    String url = getArguments().getString("url");
    return layoutView;
}

答案 3 :(得分:1)

尝试

从活动发送数据

Bundle data = new Bundle();
data.putString("url", "http://www.google.com");
MyFragment frg = new MyFragment();
frg.setArguments(data);

在Fragment中接收数据

String url = getArguments().getString("url");

答案 4 :(得分:1)

你可以通过两种方式做到这一点。

1)通过为网址设置setter方法。

public class WebViewFragment extends Fragment {
   private String url;

  public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

 }

 @Nullable
 @Override
 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
  container, @Nullable Bundle savedInstanceState) {
    return rootView;
 }
 public void setUrl(String url){
    this.url = url;

 }
}

在添加片段之前调用该方法:

 WebViewFragment fragment = new WebViewFragment();
      fragment.setUrl(url);
getSupportFragmentManager().beginTransaction() 
.replace(R.id.container,fragment).commit();

2)你可以使用那个bu意图包

  Bundle bundle = new Bundle();
   bundle.putString("url", "www.google.com");

   Fragment fragment = new Fragment();
   fragment.setArguments(bundle);
    getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();

将其解析为片段:

public class WebViewFragment扩展Fragment {        private String url;

  public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

 }

 @Nullable
 @Override
 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
  container, @Nullable Bundle savedInstanceState) {
  url = getArguments().getString("url");
    return rootView;
 }

}
相关问题