Reload the Fragment is not Working

1.3k views Asked by At

I have the following situation: A NavigationDrawer creates a Fragment to handle user information. The fragment on onCreateView() calls a Web Service and return the View. When the Web Services finishes callsback the fragment that detach and attach itself. When the Fragment is attached this second time on onCreateView() should create a View with user information. But in fact what is happening is that the View does not change.

What am I missing?

Thank you in advance,

Fragment Code:

private boolean mIsRefresh = false;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // Gets Application Context
    mContext = getActivity().getApplicationContext();

    // Set up environment to use DataBase
    mGlobal = (Global) getActivity().getApplicationContext();
    mDAOFactory = mGlobal.getDAOFactory();

    mFragment = this;

    mFragmentView = inflater.inflate(R.layout.fragment_user_info, container, false);


    buildView();

    if (mIsRefresh) {
        return mFragmentView;
    }else {
        return null;
    }
}

Here is the buildView() code:

private void buildView() {

    mEtUserName = (EditText) mFragmentView.findViewById(R.id.userName);
    mEtUserEmail = (EditText) mFragmentView.findViewById(R.id.userEmail);
    mEtPassword = (EditText) mFragmentView.findViewById(R.id.password);
    mEtPasswordConfirmation = (EditText) mFragmentView.findViewById(R.id.passwordConfirmation);
    mIbSendPassword = (ImageButton) mFragmentView.findViewById(R.id.sendPassword);
    mAcUserCountry = (AutoCompleteTextView) mFragmentView.findViewById(R.id.userCountry);
    mEtUserState = (EditText) mFragmentView.findViewById(R.id.userState);
    mEtUserCity = (EditText) mFragmentView.findViewById(R.id.userCity);
    mEtUserPhoneNumberCountry = (EditText) mFragmentView.findViewById(R.id.userPhoneNumberCountry);
    mEtUserPhoneNumberCity = (EditText) mFragmentView.findViewById(R.id.userPhoneNumberCity);
    mEtUserPhoneNumber = (EditText) mFragmentView.findViewById(R.id.userPhoneNumber);
    mCbTCAccepted = (CheckBox) mFragmentView.findViewById(R.id.cbTCAccepted);


    // User has not been created
    if (mGlobal.getUserUID() == 0) {
        getUserEmail();
        getUserLocation();

    } else {
        if (mIsRefresh) {
            formatView();
        } else {
            getDataOnServer();
        }
    }

}

Here is the code where the Web Service is called:

private void getDataOnServer () {

    mUserDAO = mDAOFactory.getUserDAO(mContext);

    final ProgressDialog progressDialog = ProgressDialog.show (getActivity(),
            getString(R.string.msgConnectingServer), getString(R.string.msgGettingData), true);

    //TODO Retirar o set abaixo
    mGlobal.setUserUID(1);

    mUserDAO.getUserByUID(mGlobal.getUserUID(), new Callback<User>() {
        @Override
        public void success(User user, Response response) {
            mUser = user;
            mIsRefresh = true;
            refresh();
        }

        @Override
        public void failure(RetrofitError error) {
            mUser = null;
            Log.e(getClass().getName(), "Retrofit Error = " + error);
            refresh();
        }

        private void refresh() {
            progressDialog.dismiss();
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.detach(mFragment)
                    .attach(mFragment)
                    .commit();

        }
    });
}

Finally the onStart()

@Override
public void onStart() {
    super.onStart();

    if (mIsRefresh) {
        mIsRefresh = false;
        getFragmentManager().beginTransaction()
                .remove(mFragment)
                .add(R.id.content_frame, mFragment)
                .commit();
    }

}
1

There are 1 answers

0
AudioBubble On BEST ANSWER

Instead of

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.detach(mFragment).attach(mFragment).commit();

Do like this:

    YourFragment mFragment = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG"); //if you are using support library
     //OR
   YourFragment mFragment = getFragmentManager().findFragmentByTag("Your_Fragment_TAG"); //if you are using support library

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.detach(mFragment);
    ft.attach(mFragment).commit();