android - DialogFragment 关闭后重新加载 fragment

标签 android android-fragments

我有Index.class作为Activity,当用户选择Profile时,它会调用 fragment 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 类中,点击注册按钮后,会显示一个对话框 fragment 类。
我想要做的是当我单击对话框 fragment 内的按钮时,如何刷新配置文件 fragment ?

对话框 fragment 类:
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() 时它崩溃了。

最佳答案

您也可以尝试刷新当前 fragment

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

关于android - DialogFragment 关闭后重新加载 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42851991/

相关文章:

android - 单击任何按钮刷新选项卡布局中的 fragment

java - 当我尝试运行 hellogridview 时出现错误

java - Android:在按钮下放置文本

android - nexus 7 不支持附件

java - 如何在 RecyclerView.ViewHolder 中引用 fragment ?请查看详情

android - 在 Activity 中实现多个警报对话框的最佳方式

java - Viewpager 显示正确的 fragment 但未初始化它

android - gradle > Task :app:externalNativeBuildDebug FAILED android_gradle_build. json(没有这样的文件或目录)

android - 如何在 pagerslidingtabstrip 中设置不同的选项卡宽度

java - 发生选项卡 fragment 错误并且 fragment 父 Activity 已被破坏