How to move from one fragment to another in a defined order in Android?

0

I have a MainActivity that contains a button, within that same Activity I have 3 fragments, the Fragment1 is the one that appears visible when I open the app, what I do now is this;

//Controlamos las acciones de los botones flotantes
@Override
public void onClick(View view) {
    int id = view.getId();
    switch (id){
        case R.id.fab:
            animateFAB();
            break;

        case R.id.fab_enviar:
          showDialog();
            break;

        case R.id.fab_agregar:
           if (container.getVisibility() == View.VISIBLE)
               segundoContainer.setVisibility(View.VISIBLE);
                container.setVisibility(View.GONE);


            break;
    }
}

In the fab_aggregate button, I make an if condition, that if the Fragment1 (in this case container) is visible, now it becomes invisible and that the Fragment2 becomes visible, until then everything works fine, now, as I can to go to Fragment3? Because if I do it in the same way it does not make sense, since if I condition that the Fragment1 is invisible and Fragment2 is visible to pass to the Fragment3, it will pass directly to that Fragment from the first one, ignoring the second one. Any ideas?

Attached screens of the app, the first image, would be the Fragment1, which as you can see is a series of inputs, the floating button to add, calls the Fragment2 (second  image) that also has a series of inputs, the third fragment is similar, with inputs.

    
asked by Harles Pereira 14.03.2018 в 14:00
source

2 answers

0

You can do it in the following way

    List<Fragment> data;
    Int cont = 0;
    @Override
    onCreate(Bundle savedInstace){
       data = new Arraylist<>();
       data.add(Fragment1)
       data.add(Fragment2)
       data.add(Fragment3)
}

    @Override
    public void onClick(View view) {
        int id = view.getId();
        FragmentManager fragmentManager = getSupportFragmentManager();
        switch (id){
            case R.id.fab:
                animateFAB();               
                break;

            case R.id.fab_enviar:
              showDialog();

                break;

            case R.id.fab_agregar:
               if (cont < data.size()){
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction()                  
fragmentTransaction.replace(R.id.fragment_container,data.get(cont++)//Aqui iria el fragment que deseas que se muestre);
                        fragmentTransaction.commit();
                         }
                    break;
            }
        }

As I mentioned before, you have to manage with a fragment manager. The most detailed information on how to do it is in the official documentation that would be this

link

Good luck and I hope it works for you

    
answered by 14.03.2018 / 21:15
source
0

I know it's a closed topic, but I'd like to leave my example ..

We create the respective classes 'we create a total of 3' that inherit from Fragment 'android.app.Fragment' with their respective method onCreateView that point or inflate your container

return inflater.inflate(R.layout.example_fragment, container, false);

On the other hand, we have our main container that will be where the respective Fragment will be housed with an ID 'small_container'

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/contenedor_pequeno"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.diego3l.fragmentsdiego.MainActivity"
    tools:layout_editor_absoluteY="81dp">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:text="Bienvenido Fragments!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/button"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="55dp"
        android:elegantTextHeight="true"
        android:text="Quiero un fragment"
        android:onClick="dameFragments"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

</android.support.constraint.ConstraintLayout>

And on the other hand we have the main class that is in charge of exchanging the respective fragments, the 3 that we have, with the respective button, which will change with each press, with a simple variable counter.

public class MainActivity extends AppCompatActivity {
    int contador = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    public void dameFragments(View view) {
        ExampleFragment1 fragment1 = new ExampleFragment1();
        ExampleFragment2 fragment2 = new ExampleFragment2();
        ExampleFragment3 fragment3 = new ExampleFragment3();

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();//comenzamos transacción

        if (contador == 0) {
            fragmentTransaction.add(R.id.contenedor_pequeno, fragment1);
            fragmentTransaction.commit();
            contador++;
        } else if (contador == 1){
            fragmentTransaction.replace(R.id.contenedor_pequeno, fragment2);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
            contador++;
        } else if (contador == 2){
            fragmentTransaction.replace(R.id.contenedor_pequeno, fragment3);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
            contador = 0;
        }

    }
}

And this is the look of the Fragments in our container

    
answered by 17.03.2018 в 13:35