我有两门课。第一个是活动,第二个是片段,其中有一些EditText。在活动中,我有一个带有async-task的子类,在方法doInBackground中,我得到了一些结果,我保存到变量中。我怎么能把这个变量从子类“我的活动”到这个片段?
当前回答
在片段和活动之间传递数据的最聪明的方法是创建一个变量,例如:
class StorageUtil {
public static ArrayList<Employee> employees;
}
然后通过onActivityCreated方法将数据从fragment传递给activity:
//a field created in the sending fragment
ArrayList<Employee> employees;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
employees=new ArrayList();
//java 7 and above syntax for arraylist else use employees=new ArrayList<Employee>() for java 6 and below
//Adding first employee
Employee employee=new Employee("1","Andrew","Sam","1984-04-10","Male","Ghanaian");
employees.add(employee);
//Adding second employee
Employee employee=new Employee("1","Akuah","Morrison","1984-02-04","Female","Ghanaian");
employees.add(employee);
StorageUtil.employees=employees;
}
现在您可以获得StorageUtil的值。来自各地的员工。 古德勒克!
其他回答
只是偶然发现了这个问题,而上面的大多数方法都是有效的。 我只是想补充一点,你可以使用事件总线库,特别是在组件(活动或片段)还没有创建的情况下,它适用于所有规模的android项目和许多用例。我个人在我在playstore上的几个项目中使用过它。
在片段和活动之间传递数据的最聪明的方法是创建一个变量,例如:
class StorageUtil {
public static ArrayList<Employee> employees;
}
然后通过onActivityCreated方法将数据从fragment传递给activity:
//a field created in the sending fragment
ArrayList<Employee> employees;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
employees=new ArrayList();
//java 7 and above syntax for arraylist else use employees=new ArrayList<Employee>() for java 6 and below
//Adding first employee
Employee employee=new Employee("1","Andrew","Sam","1984-04-10","Male","Ghanaian");
employees.add(employee);
//Adding second employee
Employee employee=new Employee("1","Akuah","Morrison","1984-02-04","Female","Ghanaian");
employees.add(employee);
StorageUtil.employees=employees;
}
现在您可以获得StorageUtil的值。来自各地的员工。 古德勒克!
使用以下接口在活动和片段之间进行通信
public interface BundleListener {
void update(Bundle bundle);
Bundle getBundle();
}
或者使用下面这个通用监听器进行双向通信使用接口
/**
* Created by Qamar4P on 10/11/2017.
*/
public interface GenericConnector<T,E> {
T getData();
void updateData(E data);
void connect(GenericConnector<T,E> connector);
}
片段显示法
public static void show(AppCompatActivity activity) {
CustomValueDialogFragment dialog = new CustomValueDialogFragment();
dialog.connector = (GenericConnector) activity;
dialog.show(activity.getSupportFragmentManager(),"CustomValueDialogFragment");
}
你也可以在onAttach(context)中将你的上下文转换为GenericConnector
在你的活动中
CustomValueDialogFragment.show(this);
在你的片段中
...
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
connector.connect(new GenericConnector() {
@Override
public Object getData() {
return null;
}
@Override
public void updateData(Object data) {
}
@Override
public void connect(GenericConnector connector) {
}
});
}
...
public static void show(AppCompatActivity activity, GenericConnector connector) {
CustomValueDialogFragment dialog = new CustomValueDialogFragment();
dialog.connector = connector;
dialog.show(activity.getSupportFragmentManager(),"CustomValueDialogFragment");
}
注意:千万不要像"".toString().toString().toString();道路
你可以在fragment中创建公共静态方法,在那里你将获得该fragment的静态引用,然后将数据传递给该函数,并将该数据设置为相同方法的参数,并通过getArgument获取fragment的oncreate方法的数据,并将该数据设置为局部变量。
我的解决方案是在片段中写一个静态方法:
public TheFragment setData(TheData data) {
TheFragment tf = new TheFragment();
tf.data = data;
return tf;
}
通过这种方式,我可以确保在任何其他可能需要使用它的操作之前,我需要的所有数据都在Fragment中。 而且在我看来它看起来更干净。
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件