2017-03-17 65 views
2

我Index.class的活动,当用户选择配置文件,它会调用片段档案DialogFragment后刷新片段驳回

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(); 
    } 

现在档案片段,当用户点击一个按钮,它会调用DevRegistration活动

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类中,点击注册按钮后,它会显示一个对话框片段类。 我想要做的是当我点击对话框片段中的按钮时,如何刷新配置文件片段?

对话片段类:

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()它崩溃。

+0

使用自定义的监听器接口 –

+0

你可以分享你的对话片段类,好吗? – Cochi

回答

0

你也可以试试这个刷新当前片段

FragmentTransaction ftran = getActivity().getFragmentManager().beginTransaction(); 
ftran .detach(this).attach(this).commit(); 
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(); 

     } 
    }); 
    } 

希望这会有所帮助。

+0

我应该将代码放在DevRegistration Activity中还是放在我的对话框片段中?点击对话框片段中的按钮后,我需要刷新代码。 –

+0

对话框片段位于DevRegistration活动中,对吗? – Cochi

+0

即时对不起,我错了,我改变了我的问题。 –