0
Good night, you guys. I have a main and 3 classes Fragment.
By accessing the 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 return 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 Ragment has the same logic. When accessing any method function, it always goes back to the first Fragment.
Voltar Method
public void VoltarAoMenu(View v) {// Metodo para voltar para MenuPrimario
Intent VoltarAoMenu = new Intent(MenuContatos.this, MainPager.class);
startActivity(VoltarAoMenu);
finish();
}
Is there any way to check which Fragment is active? If so, I will change some images in Mainpager.
I need to save and restore on the mainpager?
– Darlan Alves
Yes, in Mainpager. I understand that it is the main activity of the program.
– Márcio Oliveira
I implemented the following method: @Override public void onSaveInstanceState(Bundle outstate, Persistablebundle outPersistentState) { outstate.putSerializable("list", (Serializable) Fragments); super.onSaveInstanceState(outstate, outPersistentState); }
– Darlan Alves
And recovered in onCreate: if (savedInstanceState != null) { // Restore value of Members from saved state Fragments = (List<Fragment>) savedInstanceState.getSerializable("list"); } . But it’s still the same.
– Darlan Alves
You have to save in this savedInstanceStace variable a simple integer, which is the position of the Pager, which you take by the method getCurrentItem() from Pager. After the restore time just pass the saved value in the pager’s setCurrentItem(position) method, it will list the page that was.
– Márcio Oliveira
I edited my answer. See if it works.
– Márcio Oliveira
Now it made more sense. But it still didn’t work. It goes back to the first Fragment. I add in the list of fragments if saveInstanceState for null, right?
– Darlan Alves