gpt4 book ai didi

java - 启动 BottomSheetFragment 获取结果

转载 作者:行者123 更新时间:2023-12-02 09:50:38 26 4
gpt4 key购买 nike

我想从 BottomSheetFragment 获取结果。我尝试通过

内的 getActivity().setResult(Activity.RESULT_OK, null); 来完成此操作
    @Override
public void onDismiss(DialogInterface dialog) {
getActivity().setResult(Activity.RESULT_OK, null);
super.onDismiss(dialog);
}

但 Activity 的 onActivityResult 方法未调用。我做错了什么?

    @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(this, requestCode, Toast.LENGTH_SHORT).show();
super.onActivityResult(requestCode, resultCode, data);
}

对话框启动方法

BottomSheetDialogFragment bottomSheetFragment = new BottomSheetDialogFragment();
Bundle bundle = new Bundle();
bottomSheetFragment.setArguments(bundle);
bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());

最佳答案

onActivityResult() 用于从另一个 Activity 获取结果。

更多信息:https://developer.android.com/training/basics/intents/result

如果你想从Fragment发送一些数据到Activity,你可以通过定义一个接口(interface)来实现,或者通过将其转换到特定的Activity来调用Activity中的方法来实现。

class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
YourBottomSheetDialogFragment bottomSheetFragment = new YourBottomSheetDialogFragment();
Bundle bundle = new Bundle();
bottomSheetFragment.setArguments(bundle);
bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
}

public void setResultFromFragment(String data) {
...
}
}

/**
* Calling Activity's method from Fragment
*/
class YourBottomSheetDialogFragment extends BottomSheetDialogFragment {

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((MainActivity) getActivity()).setResultFromFragment("");
}
}

关于java - 启动 BottomSheetFragment 获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56341195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com