[英]How to get data from fragment to parent activity & pass it to back activity?
How flow goes? 流程怎么样?
Activity 1 -----> Activity 2 (containing/inside) ------> Fragment
WhatI want to achieve? 我想要实现什么?
Fragment (sends some data back to Activity 2) ----> Activity 2 (onBackPressed : collects that data & send it back to Activity 1) ---> Activity 1
How should I achieve above. 我应该如何实现上述目标。 I really don't want to use any variables/constants to cache the fragment data. 我真的不想使用任何变量/常量来缓存片段数据。 Need to know any in-built method to handle this? 需要知道任何内置方法来处理这个?
Moreover, 此外,
You should start Activity2 with startActivityForResult as below; 您应该使用startActivityForResult启动Activity2,如下所示;
Intent i = new Intent(this, Activity2.class);
startActivityForResult(i, requestCode);
And in Activity2/fragment, you should finish acitivity as below; 在Activity2 / fragment中,你应该完成以下活动;
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
getActivity().setResult(Activity.RESULT_OK,returnIntent);
getActivity().finish()
And get result in Activity1 as below; 并在Activity1中得到如下结果;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (this.requestCode == requestCode) {
if(resultCode == Activity.RESULT_OK){
//Get result
}
}
}
Hope it helps. 希望能帮助到你。
To pass data from Activity 2
to Activity 1
you can use startActivityForResult()
in Activity 1
to start Activity 2
, and onActivityResult()
in Activity 1
to receive that data. 从数据传递Activity 2
到Activity 1
可以使用startActivityForResult()
在Activity 1
开始Activity 2
,和onActivityResult()
在Activity 1
以接收该数据。
To pass data from Fragment
to Activity 2
, I'd suggest to use some interface like 要将数据从Fragment
传递到Activity 2
,我建议使用类似的界面
interface OnSomeDataListener {
void onSomeData(SomeData data);
}
Then implement a setter method for this interface to a fragment like 然后为这个接口实现一个setter方法
public void setOnSomeDataListener(OnSomeDataListener listener) {
this.listener = listener;
}
And then in Activity, when instantiating a Fragment
: 然后在Activity中,实例化Fragment
:
YourFragment fragment = new YourFragment();
fragment.setOnSomeDataListener(new OnSomeDataListener() {
void onSomeData(SomeData data) {
//return the result
Intent intent = new Intent();
intent.putExtra("data", data);
setResult(RESULT_OK, intent);
finish();
}
}
And finally, in the fragment, when you want to return some data to Activity: 最后,在片段中,当您想要将一些数据返回到Activity时:
if(listener != null) {
listener.onSomeData(dataToReturn);
}
i acheived in Following way 我按照以下方式练习
In Activity write setters and getters 在Activity中写入setter和getter
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
and in Fragment 并在片段中
filePath = ((YourActivity) getActivity()).getFilePath();
fileName=((YourActivity) getActivity()).getFileName();
if You are Using Same Fragment in More Than 1 Activity You can Create a constructor for that fragment and Pass context and check context is of which activity 如果您在多个活动中使用相同的片段您可以为该片段创建构造函数并传递上下文并检查上下文是哪个活动
public class BookmarkFragment extends Fragment {
private String filePath;
private String fileName;
Context contextCheckClass;
public BookmarkFragment(Context ctx ) {
this.contextCheckClass=ctx;
}
@SuppressLint("InflateParams")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
group= (View)inflater.inflate(R.layout.fragment_bookmark, null);
if(contextCheckClass instanceof FirstReaderScreen){
filePath = ((FirstReaderScreen) getActivity()).getFilePath();
fileName=((FirstReaderScreen) getActivity()).getFileName();
// ispurchased=((FirstReaderScreen) getActivity()).isIspurchased();
}
else if(contextCheckClass instanceof MainReaderScreen){
filePath = ((MainReaderScreen) getActivity()).getFilePath();
fileName=((MainReaderScreen) getActivity()).getFileName();
// ispurchased=((MainReaderScreen) getActivity()).isIspurchased();
}
return group;
}
for calling fragment 用于调用片段
BookmarkFragment bookmarkFragment=new BookmarkFragment(FirstReaderScreen.this);
getSupportFragmentManager()
.beginTransaction()
.add(R.id.LL_Fragment, bookmarkFragment)//LL_fragment is container
.addToBackStack(null)
.commit();
From your FirstActivity call the SecondActivity using startActivityForResult() method 从您的FirstActivity使用startActivityForResult()方法调用SecondActivity
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
In your SecondActivity set the data which you want to return back to FirstActivity onBackPressed of SecondActivity. 在SecondActivity中,将要返回的数据设置为SecondActivity onBackPressed的SecondActivity。
@Override
public void onBackPressed() {
super.onBackPressed();
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
returnIntent.putExtra("bool",true);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
Now in your FirstActivity class write following code for the onActivityResult() method. 现在,在您的FirstActivity类中,为onActivityResult()方法编写以下代码。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
boolean bool = data.getBooleanExtra("bool");
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//
To send data from Fragment to Second activity, you can use interface callback 要将数据从Fragment发送到Second活动,您可以使用接口回调
public interface sendDataListener
{
void sendData(boolean foo);
}
Implement this listener to Second activity 将此侦听器实现为Second活动
Try to do this: 尝试这样做:
public class MyFragment extends Fragment{
private MyFragmentCommunicator myFragmentCommunicator;
....
myFragmentCommunicator.sendDataToActivity(name, image, isSend);
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
myFragmentCommunicator = (MyFragmentCommunicator)activity;
}
}
then declare your interface: 然后声明你的界面:
public interface MyFragmentCommunicator{
void sendDataToActivity(String name, String image, boolean isSend);
}
and then in your Activity do this: 然后在你的Activity中执行以下操作:
public class MyActivity extends AppCompatActivity implements MyFragmentCommunicator{
@Override
public void sendDataToActivity(String name, String image, String price) {
//Manipulate the data
}
}
Hope it helps!!! 希望能帮助到你!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.