DialogFragment解散后重新加载片段

时间:2017-03-17 07:49:17

标签: android android-fragments

我将Index.class作为Activity,当用户选择Profile时,它将调用片段Profile

  if (id == R.id.nav_profile){
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        transaction.setCustomAnimations(R.anim.enter,R.anim.exit,R.anim.enter,R.anim.exit);
        transaction.replace(R.id.flContent, new Profile(), "ProfileFragment");
        transaction.addToBackStack(null);
        viewPager.getAdapter().notifyDataSetChanged();
        transaction.commit();
    }

现在在Profile Fragment中,当用户点击按钮时,它将调用DevRegistration Activity

 case 1:
            btnBeDeveloper.setText("Developer Console");
            btnBeDeveloper.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                     Intent index = new Intent(getActivity(), DevRegistration.class);
                    startActivity(index);
                }
            });
            break;

然后在DevRegistration类中,单击注册按钮后,它将显示一个对话框片段类。 我想要做的是当我单击对话框片段内的按钮时,如何刷新配置文件片段?

Dialog Fragment Class:

public class df_SuccessDevRegistration extends DialogFragment {

Button btnDevGoProfile;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    View rootView = inflater.inflate(R.layout.fragment_success_developer_registration, container, false);

    btnDevGoProfile = (Button) rootView.findViewById(R.id.btnDevGoProfile);

    btnDevGoProfile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
            getFragmentManager().popBackStack();
            getActivity().finish();

            Profile fragment = (Profile)
                    getSupportFragmentManager().findFragmentByTag("ProfileFragment");

            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction()
            transaction.detach(fragment);
            transaction.attach(fragment);
            transaction.commit();

        }
    });

    return rootView;
}

}

顺便说一句,我不知道为什么getSupportFragmentManager不起作用。它显示错误无法解析方法...当我使用getFragmentManager()时它崩溃了。

2 个答案:

答案 0 :(得分:0)

在您的开发注册活动中,获取您的片段:

Profile profileFragment = getSupportFragmentManager().findFragmentByTag("ProfileFragment");

要刷新它,您可以尝试:

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.detach(profileFragment);
transaction.attach(profileFragment);
transaction.commit();

编辑:

你能试试吗?

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    View rootView = inflater.inflate(R.layout.fragment_success_developer_registration, container, false);

    btnDevGoProfile = (Button) rootView.findViewById(R.id.btnDevGoProfile);



    return rootView;
}

@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        btnDevGoProfile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getActivity().getSupportFragmentManager().popBackStack();


            Profile fragment = (Profile)
                    getActivity().getSupportFragmentManager().findFragmentByTag("ProfileFragment");

            FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction()
            transaction.detach(fragment);
            transaction.attach(fragment);
            transaction.commit();

            getActivity().finish();
            dismiss();

        }
    });
    }

希望这有帮助。

答案 1 :(得分:0)

您也可以尝试刷新当前片段

 FragmentTransaction ftran = getActivity().getFragmentManager().beginTransaction();
 ftran .detach(this).attach(this).commit();