fragment返回onActivityResult的data为空
时间: 2024-06-10 22:08:37 浏览: 15
当Fragment返回的onActivityResult的data为空时,可以通过以下步骤解决:
1. 确保在Fragment所属的Activity中重写了onActivityResult方法,并且在重写的方法中调用了super.onActivityResult(requestCode, resultCode, data)。
2. 在Activity中重写onActivityResult方法时,通过getSupportFragmentManager().getFragments()获取到所有的Fragment,并遍历它们,调用各自的onActivityResult方法。
3. 在Fragment中重写onActivityResult方法,确保在方法中获取到activity的data。
相关问题
android fragment onactivityresult
在 Android 中,Fragment 也可以通过 startActivityForResult() 方法启动另外一个 Activity,并通过 onActivityResult() 方法接收返回的数据。在 Fragment 中使用 startActivityForResult() 方法时,要注意以下几点:
1. 需要在 Fragment 中重写 onActivityResult() 方法,而不是在 Fragment 所依附的 Activity 中重写。
2. 在 Fragment 中启动 Activity 时,需要使用 getActivity().startActivityForResult() 方法,而不是直接使用 startActivityForResult() 方法。
3. 在 Fragment 中接收返回数据时,需要调用 getActivity().onActivityResult() 方法,而不是直接调用 Fragment 的 onActivityResult() 方法。
例如,以下是一个在 Fragment 中使用 startActivityForResult() 方法的示例:
```
public class MyFragment extends Fragment {
private static final int REQUEST_CODE = 100;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_my, container, false);
}
public void startMyActivity() {
Intent intent = new Intent(getActivity(), MyActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
String result = data.getStringExtra("result");
// 处理返回数据
}
}
@Override
public void onResume() {
super.onResume();
if (getActivity() instanceof MyActivity) {
((MyActivity) getActivity()).setResult(Activity.RESULT_OK, new Intent().putExtra("result", "Hello World!"));
getActivity().finish();
}
}
}
```
在上面的示例中,我们在 Fragment 中启动了一个名为 MyActivity 的 Activity,并在 Activity 中模拟了返回数据的操作。当 Activity 被销毁时,会自动回调 Fragment 的 onActivityResult() 方法,我们可以在该方法中接收返回的数据并进行处理。
Fragment的onActivityResult() 方法中怎么使用findViewById
在 Fragment 的 onActivityResult() 方法中,可以使用 getView() 方法获取 Fragment 的根视图,然后使用该根视图的 findViewById() 方法来查找视图元素。
例如,以下是一个在 Fragment 中使用 getView() 和 findViewById() 方法的示例:
```
public class MyFragment extends Fragment {
private static final int REQUEST_CODE = 100;
private TextView mResultTextView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_my, container, false);
mResultTextView = (TextView) rootView.findViewById(R.id.result_text_view);
return rootView;
}
public void startMyActivity() {
Intent intent = new Intent(getActivity(), MyActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
String result = data.getStringExtra("result");
mResultTextView.setText(result);
}
}
}
```
在上面的示例中,我们在 Fragment 的 onCreateView() 方法中获取了 Fragment 的根视图,并使用根视图的 findViewById() 方法来查找名为 result_text_view 的 TextView。在 onActivityResult() 方法中,我们可以使用该 TextView 来显示返回的数据。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)