activity 实现底部弹窗的BottomSheetDialog,并在activity中获取 BottomSheetDialog所在的fragmente 实例
时间: 2024-03-02 16:53:38 浏览: 146
要实现在 Activity 中获取 BottomSheetDialog 所在的 Fragment 实例,可以通过在 BottomSheetDialog 中传递 FragmentManager 和 tag 的方式来实现。
具体实现步骤如下:
1. 在 Activity 中创建 BottomSheetDialog,并设置 tag。
```java
MyBottomSheetDialog bottomSheetDialog = new MyBottomSheetDialog();
bottomSheetDialog.show(getSupportFragmentManager(), "bottomSheetDialogTag");
```
2. 在 BottomSheetDialog 中,通过 getTag() 方法获取设置的 tag。
```java
String tag = getTag();
```
3. 在 BottomSheetDialog 中,通过 FragmentManager 的 findFragmentByTag() 方法获取当前 Fragment 的实例。
```java
Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(tag);
```
4. 在 BottomSheetDialog 中可以使用获取到的 currentFragment 实例进行相关操作。
```java
public class MyBottomSheetDialog extends BottomSheetDialog {
public MyBottomSheetDialog() {
super(requireContext(), R.style.BottomSheetDialogTheme);
}
@Override
public void onStart() {
super.onStart();
String tag = getTag();
Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(tag);
// 使用 currentFragment 进行相关操作
}
}
```
通过这样的方式,在 Activity 中可以获取到 BottomSheetDialog 所在的 Fragment 实例,并进行相关操作。
阅读全文
相关推荐












