如何解决如何在活动 1 中调用的对话框中显示活动 2 的内容?
我基本上有一个活动,其中有针对不同内容的多种布局。例如,有一个 switch 语句负责在不同的内容视图之间进行切换。每个视图都包含一个参数,我通过选择一个单选按钮来确定该参数。然后在我之前活动的 onActivityResult 中检索此选择。
我想做同样的事情,而是在这两个活动之间来回切换,我希望带有参数选择的活动在对话框中显示其内容。因此,换句话说,当我单击 Activity 1 中的某个内容时,会弹出一个对话框,其中包含 Activity 2 的其中一个视图的内容。
情况如何:
活动 1------>活动 2 Activity2 数据---存储---> Activity 1
我现在想要的:
Activity 1 ---打开对话框----> 对话框包含Activity 2的参数选择 我做出选择,当对话框关闭时,数据存储在活动 1 中。
这可能吗?
如果是这样怎么办?如果不是,我可以使用的另一种方法是什么?
解决方法
您可以使用 LocalBroadcastManager
活动 1 中 oncreate() 方法的代码:
LocalBroadcastManager.getInstance(this).registerReceiver(tempReceiver,new IntentFilter("dialogData"));
Activity1 的 onCreate() 方法之外
private BroadcastReceiver tempReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context,Intent intent) {
Bundle bundle=intent.getExtras();
/* show dialog and
you can get values same like bewlow code*/
if(bundle!=null) {
String dialogData;
try {
dialogData= bundle.getString("your_key");
}catch (Exception e){
e.printStackTrace();
}
}
}
};
Activity2 中 onBackpress()
方法下的代码:
Intent intent = new Intent("dialogData");
intent.putExtra("your_key","yourVaue");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
finish();
在此代码中,yourValue
是所选视图的值
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。