从fragmentA打开FragmentB +方法

时间:2019-04-11 08:36:23

标签: android

我需要使用FragmentA打开FragmentB + FragmentsB方法

更准确地说,我所拥有的和我所需要的:

  1. EmptyProfile.class,带有按钮openProfile()。
  2. 具有openProfileDialog()方法的ArtistProfileView.class

因此,当用户使用EmptyProfile.class时,他按下了一个按钮openProfile,它将调用:

ArtistProfileView.class +自动打开openProfileDialog()。

什么,我已经尝试过了。我尝试用替换的片段打开ArtistProfileView.class。它工作正常,但是在这种情况下,我将只打开ArtistProfileView.class,然后用户应自己单击openProfileDialog。

  btn2.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  AppCompatActivity activity = (AppCompatActivity) view.getContext();
  ArtistProfileView myFragment = new ArtistProfileView();
  activity.getSupportFragmentManager().beginTransaction()
  .replace(((ViewGroup)(getView().getParent())).getId(), myFragment)
  .commit();
 }
});

这部分代码正在使用,以打开profileDialog();。在ArtistProfileView.class

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ivEditPersonal
my_tag = 1
dialogPersonalProfile();
 break;

所以,我认为,当我使用EmptyProfile.class时,我必须调用类似的东西

myFragment.my_tag = 1 dialogPersonalProfile(); 还是我总体上做错了什么? 谢谢!

当用户使用EmptyProfile.class时,他可以按Fill the Profile(填充配置文件)按钮,并自动打开ArtistProfileView,并自动打开dialogPersonalProfile。

2 个答案:

答案 0 :(得分:1)

输入有关您需要捆绑打开对话框的事实的信息:

FragmentsB fragment = new FragmentsB();
Bundle bundle = new Bundle();
bundle.putBool("START_DIALOG", true);
fragment.setArguments(bundle);

然后在FragmentsB中使用以下方法检索数据(例如在onCreate()方法中)

Bundle bundle = this.getArguments();
if (bundle != null) {
       if (bundle.getBool("START_DIALOG", false){
            showDialogMethod();
        }
}

答案 1 :(得分:0)

如果情况总是如此,则您希望在ArtisProfileView中显示Dialog,因为可以从任何地方调用片段。 尝试在ArtistProfileView的onResume方法中调用dialogPersonalProfile()函数。同样不要忘记在onPause中关闭对话框。

否则请发送带有布尔值标志的捆绑软件并使用它。