Fragments in the TabLayout's ViewPager disappear on the second call

0

Come on!

I have an application that has a DrawerMenu (Menu Sliding), clicking on the Edit Profile item, calls a new Fragment ( EditFragment.class ), So, in this Fragment you have a TabLayout with a ViewPager , which contains 2 Tabs. Each Tab is populated with 1 Fragment ( EditGeraisFragment.class and EditSenhaFragment.class ).

When I first click on the Menu item, everything appears normally, it is called the EditFragment and the other 2 Fragments in the ViewPager normally appear, but when I click in another Menu item, and I click the Profile Edit Menu again , the Fragments in ViewPager no longer appear, / p>

Here are the codes:

Call the DrawerMenu .

.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                @Override
                public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                    if (drawerItem != null) {

                        Fragment frag = null;

                        if(drawerItem.getIdentifier() == 1) {
                            frag = new PerfilFragment();
                            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                            ft.replace(R.id.rl_fragment_container, frag, "mainFrag");
                            ft.commit();

                            mToolbar.setTitle(((PrimaryDrawerItem) drawerItem).getName().toString());
                        } else if(drawerItem.getIdentifier() == 2) {
                            frag = new DepositosFragment();
                            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                            ft.replace(R.id.rl_fragment_container, frag, "mainFrag");
                            ft.commit();

                            mToolbar.setTitle(((PrimaryDrawerItem) drawerItem).getName().toString());
                        } else if(drawerItem.getIdentifier() == 3) {
                            frag = new EmprestimosFragment();
                            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                            ft.replace(R.id.rl_fragment_container, frag, "mainFrag");
                            ft.commit();

                            mToolbar.setTitle(((PrimaryDrawerItem) drawerItem).getName().toString());
                        } else if(drawerItem.getIdentifier() == 4) {
                            frag = new EditFragment();
                            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                            ft.replace(R.id.rl_fragment_container, frag, "mainFrag");
                            ft.commit();

                            mToolbar.setTitle(((PrimaryDrawerItem) drawerItem).getName().toString());
                        } else if(drawerItem.getIdentifier() == 7) {
                            PrefsUsuario.clearPrefs(mContext);

                            Intent abreLogin = new Intent(mContext, LoginActivity.class);
                            startActivity(abreLogin);
                            finish();
                        }
                    }

                    return false;
                }
            })

EditFragment.class

public class EditFragment extends Fragment {

private TabLayout tabLayout;
private ViewPager viewPager;
private ImageView profileImage, addPhoto;
private TextView profileName;
public static String photoImage, urlImageProfile;
private Context mContext;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_edit, container, false);

    viewPager = (ViewPager) view.findViewById(R.id.vp_editperfil);
    viewPager.setAdapter(new EditAdapter(getActivity().getSupportFragmentManager(), mContext));

    tabLayout = (TabLayout) view.findViewById(R.id.tabs_editperfil);
    tabLayout.setupWithViewPager(viewPager);

    profileImage = (ImageView) view.findViewById(R.id.profile_image);
    addPhoto = (ImageView) view.findViewById(R.id.add_photo);

    photoImage = PrefsUsuario.getPhoto(mContext);
    urlImageProfile = Funcoes.BuscarUriPhoto(photoImage);

    Picasso.with(mContext).load(urlImageProfile).placeholder(R.drawable.imagem_semfoto).into(profileImage);
    Picasso.with(mContext).load(R.drawable.icon_add).placeholder(R.drawable.imagem_semfoto).into(addPhoto);

    return view;
}

@Override
public void onAttach(Context context) {
    this.mContext = context;
    super.onAttach(mContext);
}
}
  • Below the 2 ViewPager Fragments

EditGeraisFragment.class

public class EditGeraisFragment extends Fragment {

private EditText textNome, textCPF, textEmail, textTelefone;
private Button botaoEnviarGerais;
private Context mContext;
//private static final String URLEnviarGerais = "http://www.caixinhadosmotoristas.com.br/validacao.php?acao=info-cliente";
//private RequestQueue requestQueue;
//private StringRequest request;
//private int idcliente;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_editgerais, container, false);

    textNome = (EditText) view.findViewById(R.id.textNome);
    textCPF = (EditText) view.findViewById(R.id.textCPF);
    textEmail = (EditText) view.findViewById(R.id.textEmail);
    textTelefone = (EditText) view.findViewById(R.id.textTelefone);
    botaoEnviarGerais = (Button) view.findViewById(R.id.buttonEnviarGerais);

    textNome.setText(PrefsUsuario.getNome(mContext));
    textCPF.setText(PrefsUsuario.getCpf(mContext));
    textCPF.setEnabled(false);
    textEmail.setText(PrefsUsuario.getEmail(mContext));
    textTelefone.setText(PrefsUsuario.getTelefone(mContext));

    //idcliente = PrefsUsuario.getIdCliente(mContext);

    return view;
}

@Override
public void onAttach(Context context) {
    this.mContext = context;
    super.onAttach(mContext);
}
}

EditSenhaFragment.class

public class EditSenhaFragment extends Fragment {

private EditText textSenhaAtual, textSenhaNova, textConfirmarSenha;
private Button botaoEnviarSenha;
private Context mContext;
//private static final String URLEnviarSenha = "http://www.caixinhadosmotoristas.com.br/validacao.php?acao=info-cliente";
//private RequestQueue requestQueue;
//private StringRequest request;
//private int idusuario;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_editsenha, container, false);

    textSenhaAtual = (EditText) view.findViewById(R.id.textSenhaAtual);
    textSenhaNova = (EditText) view.findViewById(R.id.textSenhaNova);
    textConfirmarSenha = (EditText) view.findViewById(R.id.textConfirmarSenha);
    botaoEnviarSenha = (Button) view.findViewById(R.id.buttonEnviarSenha);

    textSenhaAtual.setText(PrefsUsuario.getSenha(mContext));
    textSenhaAtual.setEnabled(false);

    //idusuario = PrefsUsuario.getIdUsuario(mContext);

    return view;
}

@Override
public void onAttach(Context context) {
    this.mContext = context;
    super.onAttach(mContext);
}
}
  • Below is the ViewPager Adapter

EditAdapter.class

public class EditAdapter extends FragmentPagerAdapter {
private Context mContext;
private String[] titles = {"GERAIS", "SENHA"};

public EditAdapter(FragmentManager fm, Context context) {
    super(fm);
    mContext = context;
}

@Override
public Fragment getItem(int position) {
    Fragment frag = null;

    if(position==0) {
        frag = new EditGeraisFragment();
    } else if(position==1) {
        frag = new EditSenhaFragment();
    }

    Bundle bundle = new Bundle();
    bundle.putInt("position", position);

    frag.setArguments(bundle);

    return frag;
}

@Override
public int getCount() {
    return titles.length;
}

@Override
public CharSequence getPageTitle(int position) {
    return (titles[position]);
}
}
    
asked by anonymous 25.10.2017 / 21:03

1 answer

1

I found this answer in Stack Overflow in English and it helped me with my problem.

Answer to the above question

I will explain below what I did, in EditFragment.class, replace this line below:

viewPager.setAdapter(new EditAdapter(getActivity().getSupportFragmentManager(), mContext));

By the line below:

viewPager.setAdapter(new EditAdapter(getChildFragmentManager(), mContext));

Now my code is working perfectly.

    
26.10.2017 / 01:35