Fragments in Tablayout’s Viewpager disappear on the second call

Asked

Viewed 126 times

0

Come on!

I have an app that has a Drawermenu (Sliding Menu), when clicking on the Edit Profile item, calls a new Fragment (Editfragment.class), up to there beauty, in that Fragment has a Tablayout with a Viewpager, containing 2 Tabs. Each Tab is filled with 1 Fragment (Editgeraisfragment.class and Editsenhafragment.class).

When I first click on the Menu item, everything normally appears, is called the Editfragment and normally appears the other 2 Fragments in the Viewpager, but when I click on another Menu item, I click again on Edit Profile Menu, the Fragments in the Viewpager no longer appear, remain blank.

Follow below the codes:

Call from the click on 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 Fragments of Viewpager

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 follows 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]);
}
}

1 answer

1


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

Answer to the above question

I will explain below what I did, in Editfragment.class, replaces this line below:

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

Down the line:

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

Now my code is working perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.