android - 将参数传递给 XML 布局中定义的 fragment

标签 android android-fragments android-appcompat

public class ActivityA extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);
    }
}

activity_one.xml
<fragment
    android:id="@+id/fragment"
    class="com.emoontech.waternow.FragmentA"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

fragment A
public class FragmentA extends Fragment{
    public static FragmentEditEvent newInstance(String param1, String param2) {
        FragmentEditEvent fragment = new FragmentEditEvent();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }
}

//从另一个Activity点击按钮
Intent intent = new Intent(this, ActivityA.class);
intent.putString("param1","val1");
intent.putString("param2","val2");
startActivity(intent);

//如何将这两个值发送给FragmentA?

最佳答案

我在那里回答https://stackoverflow.com/a/32748751/3301009在 Activity 之间共享数据非常通用,也可以为 fragment 工作

关于android - 将参数传递给 XML 布局中定义的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32747114/

相关文章:

java - 在Android中使用Bundle将ArrayList类对象传递给另一个 fragment

android - StaggeredGridLayoutManager 中 itemTouchHelperCallback 中的困惑图 block

java - 如何在Android Studio上设置webdav?

android - java.lang.IllegalStateException(在onSaveInstanceState之后不能执行这个 Action )

android - 扩展 appcompat textview 样式

android - appcompat 值-v21\themes_base.xml 错误

Android AppCompat v21 提供SwitchCompat 不提供SwitchCompatPreference

android - Angular 错误 : StaticInjectorError (Platform: core)[e -> t]:

android开始和结束动画的图片

android - 实例化新 Android fragment 的最佳实践