【问题标题】:How to pass data from one fragment to another? [duplicate]如何将数据从一个片段传递到另一个片段? [复制]
【发布时间】:2016-03-17 07:31:06
【问题描述】:
startActivity(new Intent(SecondScreen.this,FragmentOne.class).
putExtra("key", VideoFullUrl)); 

并且在扩展 android.support.v4.app.Fragment 的 FragmentOne.class 中

      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    String   url = getIntent().getStringExtra("key");//here showing compilation error "cannot resolve method getIntent "
   //somecode....

【问题讨论】:

标签: android android-fragments bundle


【解决方案1】:

Fragment 到 Fragment 设置并获取参数:

开始活动:

 int friendId = 2; //value to pass as extra 
 i = new Intent(firstActivity, SecondActivity.class);
 i.putExtra("friendsID", friendId);
 firstActivity.startActivity(i);

第二个活动:

 Fragment_A mFragment_A = new Fragment_A();
 mFragment_A.setArguments(getIntent().getExtras());

片段_A:

Bundle bundle = new Bundle();
String Item = getArguments().getString("friendsID");
bundle.putInt("friendsID", Integer.parseInt(Item));

// code

Fragment_B mFragment_B = new Fragment_B();
mFragment_B.setArguments(bundle);

片段_B:

Bundle bundle = getArguments();
int value = bundle.getInt("friendsID");

Log.e("value Fragment get Argument ", "friendsID :" + value);

这对我有用,试试这个可能对你有帮助。

【讨论】:

  • 请将其标记为答案。如果您的问题解决了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多