Back to the screen fragment

0

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.

    
asked by anonymous 25.04.2017 / 23:07

1 answer

0

I'm imagining that these features you're quoting are in other activities. Then when you leave the main Activity (which has the pager) it is destroyed and when you return to it, it is recreated (and in the case of the pager, it loads the first Fragment). This is the life cycle of Activity.

What you can do is save the pager's position before the Activity is destroyed (onSaveInstanceState method) and restore it to onCreate.

It looks like this:

1) Declare your pager outside of OnCreate.

ViewPager pager;

2) Declare the OnSaveInstant method to save the Pager position

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putInt(pager.getCurrentItem(), "pagerPosition");
    super.onSaveInstanceState(savedInstanceState);
}

3) Within the OnCreate, at the end, restore the position of the pager:

if (savedInstanceState != null)
    pager.setCurrentItem(savedInstanceState.getInt("pagerPosition"));
    
26.04.2017 / 03:02