简体   繁体   English

更新来自另一个活动的数据片段

[英]Update data fragment from another activity

It's seem to be easily but I cannot find solution although i have checked at: How to update Android fragment from activity? 这似乎很容易,但是尽管我检查了以下内容,但找不到解决方案: 如何从活动中更新Android片段? How to change fragment's textView's text from activity 如何从活动中更改片段的textView的文本

My problem is: 我的问题是:

I have 4 class: 我有4节课:

  • MainActivity.java MainActivity.java
  • HistoryFragment.java HistoryFragment.java
  • ConversationDialog.java ConversationDialog.java
  • CallActivity.java CallActivity.java

In MainActivity.java, It content 2 Fragment includes HistoryFragment and ContactFragment. 在MainActivity.java中,其内容2 Fragment包括HistoryFragment和ContactFragment。 From HistoryFragment, I call ConversationDialog to show dialog, input to dialog and press OK to open CallActivity. 从HistoryFragment中,我调用ConversationDialog来显示对话框,输入对话框,然后按OK打开CallActivity。

Now, I want when I press back from CallActivity, I must update HistoryFragment with new data. 现在,我想从CallActivity中按回去时,必须用新数据更新HistoryFragment。

But after research, I cannot do it. 但是经过研究,我做不到。

Please help me to do this. 请帮我做到这一点。

The onResume() function of the MainActivity will be get called when you are pressing back button from CallActivity. 当您从CallActivity按下返回按钮时,将调用MainActivity的onResume()函数。 Here the logic goes. 逻辑在这里。

Do the following steps, 执行以下步骤,

1.Implement a public method(ie, updateContent()) in HistoryFragment to update your content. 1.在HistoryFragment中实现一个公共方法(即updateContent())以更新您的内容。

2.Get the HistoryFragment instance from onResume() of MainActivity 2,从MainActivity的onResume()获取HistoryFragment实例

FragmentManager fragmentManager = activity.getSupportFragmentManager();
HistoryFragment aFragment=fragmentManager.findFragmentByTag("Place the History fragment TAG here which you have used to load before");

3.After getting the instance, call the relevent method through instance. 3.获取实例后,通过实例调用relevent方法。

aFragment.updateContent()

When starting the CallActivity use startActivityForResults like: 启动CallActivity时,请使用startActivityForResults,例如:

Intent intent = new Intent(this,  CallActivity.class);
startActivityForResult(intent, 1);

Where 1 is the request code that will be used later to get back the data you are requesting. 其中1是请求代码,稍后将用于取回您请求的数据。

Then in your onBackPress function in Call Activity 然后在Call Activity中的onBackPress函数中

Intent intent = new Intent();
intent.putExtra("id","value")
setResult(RESULT_OK, intent);        
finish();

Then do this to get your data in the previous Activity 然后执行此操作以获取上一个活动中的数据

public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
     if(resultCode == RESULT_OK){
      String stredittext=data.getStringExtra("id");
  }
} 

Use the "id" is the identifier of your value which is "value" .I think now you can easily do the rest of adding your data to the history fragment using public variables. 使用“ id”是您的的标识符,即“值” 。我想现在您可以轻松地完成使用公共变量将数据添加到历史片段的其余工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM