Press the button of a screen that is a Fragment and go to another Fragment. How do you do?

Asked

Viewed 832 times

1

I have a class like Fragment and would like when the user clicks the button to go to another screen that is Fragment.. how can I do this?

public class AlertaFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {

        View view =  inflater.inflate(R.layout.fragment_alerta, container, false);

        FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        return view;
    }
}

@EDIT

I put it like this: It highlights the argument in red...

final FragmentTransaction ft = getFragmentManager().beginTransaction();
CadastroAlertaFragment cadastro = new CadastroAlertaFragment();
ft.replace(R.id.cadastro_frag, cadastro, cadastro.getTag());
ft.commit();

Error:

Error:(167, 48) error: incompatible types: CadastroAlertaFragment cannot be converted to Fragment
  • The new fragment will take the same place as the current fragment?

  • Not.............

  • How is your class CadastroAlertaFragment?

  • public class Cadastre alertafragment extends Fragment {}

  • and has a registration layout for it called fragment_cadastro_alerta.xml and put an id in the android layout:id="@+id/cadastro_frag"

2 answers

3


Within the method onClick from your button, you can do this using the method replace() of FragmentTransaction. Behold:

final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
ft.replace(R.id.details, new NewFragmentToReplace(), "NewFragmentTag"); 
ft.commit(); 

And if you want to go back to the Fragment previous, see more details on the method addToBackStack().

  • I’m researching here and I’ll tell you more soon.

  • But remember I’m in a Ragment and I want to go to another screen....

  • @Aline you are in one Ragment and want to go to another Ragment is that right?! Or you want to an Activity? Now you confused me

  • Ack sorry. I’m in a Fragment and I want to go to another Fragment

  • I’m new on android.

  • 2

    @Aline then that’s what I did in the reply, using the replace method()

  • 1

    Worked.......

Show 2 more comments

3

There’s another way to do that. And maybe it’s simpler than it looks.

Fragment

View.OnClickListener onClickHandler = new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        FragmentActivity mainActivity = getActivity();

        if(mainActivity instanceof suaActivityPrincipal)
        ((suaActivityPrincipal) mainActivity).setCurrentItemPager(1); // 1 = ID do fragment
    }

};

Activity

public void setCurrentItemPager(int id){
    viewPager.setCurrentItem(id); // viewPager = substitua pelo seu viewPAger
}

It’s a little simpler to do that. He makes a reference to Activity father to change the fragment current.

Change the line: setCurrentItemPager(1) for the ID of the fragment that you want to be shown.

Browser other questions tagged

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