Back to Canvas Ragment

Asked

Viewed 404 times

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.

1 answer

0

I’m imagining that these features you mention 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, loads the first Fragment). So is the life cycle of Activity.

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

Do so:

1) Declare your pager out of Oncreate.

ViewPager pager;

2) Declare Onsorbant method to save Pager position

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

3) Inside the Oncreate, at the end, restore the pager position:

if (savedInstanceState != null)
    pager.setCurrentItem(savedInstanceState.getInt("pagerPosition"));
  • I need to save and restore on the mainpager?

  • Yes, in Mainpager. I understand that it is the main activity of the program.

  • I implemented the following method: @Override public void onSaveInstanceState(Bundle outstate, Persistablebundle outPersistentState) { outstate.putSerializable("list", (Serializable) Fragments); super.onSaveInstanceState(outstate, outPersistentState); }

  • 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.

  • 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.

  • I edited my answer. See if it works.

  • 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?

Show 2 more comments

Browser other questions tagged

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