Good evening, everyone. I have a main and 3 classes fragment.
When accessing main, I can slide to each fragment correctly. However, in each fragment I have functionalities. If I access one of these functions and try to go back it always opens the first fragment.
PagerAdapter
public class PagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
// TODO Auto-generated constructor stub
this.fragments = fragments;
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return this.fragments.get(arg0);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
MainPager
public class MainPager extends FragmentActivity {
private PagerAdapter pagerAdapter;
private Button buttonVoltar, buttonAnterior, buttonProximo;
private MenuPrimario menuPrimario;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainpager);
buttonVoltar = (Button) findViewById(R.id.buttonVoltarPrincipal);
buttonAnterior = (Button) findViewById(R.id.buttonAnterior);
buttonProximo = (Button) findViewById(R.id.buttonProximo);
buttonVoltar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainPager.this, TelaInicial.class);
startActivity(intent);
finish();
}
});
List<Fragment> fragments = new Vector<>();
fragments.add(Fragment.instantiate(this, MenuPrimario.class.getName()));
fragments.add(Fragment.instantiate(this, MenuSecundario.class.getName()));
fragments.add(Fragment.instantiate(this, MenuTerciario.class.getName()));
pagerAdapter = new PagerAdapter(this.getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
pager.setAdapter(pagerAdapter);
}
@Override
public void onBackPressed() {
Intent intent = new Intent(MainPager.this, TelaInicial.class);
startActivity(intent);
finish();
}
MenuPrimario
public class MenuPrimario extends Fragment {
private LinearLayout containerTelefone, containerContato, containerMensagem, containerCamera;
private ImageView imgTelefone, imgContato, imgMensagem, imgCamera;
private TextView textTelefone, textContato, textMensagem, textCamera;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
return inflater.inflate(R.layout.activity_menuprimario, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
containerTelefone = (LinearLayout) view.findViewById(R.id.container_telefone);
containerTelefone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AbrirTelefone();
}
});
containerContato = (LinearLayout) view.findViewById(R.id.container_contato);
containerContato.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AbrirContato();
}
});
containerMensagem = (LinearLayout) view.findViewById(R.id.container_mensagem);
containerMensagem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AbrirMensagem();
}
});
containerCamera = (LinearLayout) view.findViewById(R.id.container_camera);
containerCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AbrirCamera();
}
});
}
The other 2 fragment has the same logic. When accessing any function of the methods, it always returns to the first fragment.
Back Method
public void VoltarAoMenu(View v) {// Metodo para voltar para MenuPrimario
Intent VoltarAoMenu = new Intent(MenuContatos.this, MainPager.class);
startActivity(VoltarAoMenu);
finish();
}
Can you verify which fragment is active? If so, I will change some images in the MainPager.