How to manipulate data from a Fragment when sliding to the next one?

Asked

Viewed 37 times

0

Explaining:

I have 3 Fragments in a viewpager2, for you to make slides.

In a specific Fragment, I have some data in edittext and etc. When sliding to the next Fragment, I need this data to be written or manipulated in some way. But the next button or the slide action itself, is done in Activity or viewpager2. How to have edittexts manipulated after this action?

Here is my Activity which instantiates the Fragments:

public class ScreenSlideActivity extends FragmentActivity {

    private ViewPager2 mPager;
    private FragmentStateAdapter pagerAdapter;
    private Button btn_pular;
    private Button btn_voltar;
    ArrayList<Fragment> fragments = new ArrayList<>();

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

        btn_pular = findViewById(R.id.btn_puler_pager);
        btn_voltar = findViewById(R.id.btn_voltar_pager);

        // Instantiate a ViewPager and a PagerAdapter.
        mPager = findViewById(R.id.pager);
        pagerAdapter = new ScreenSlidePagerAdapter(this, initiateFragments());
        mPager.setAdapter(pagerAdapter);
        mPager.setPageTransformer(new ZoomOutPageTransformer());

    }

    private ArrayList<Fragment> initiateFragments() {

        fragments.add(ScreenSlidePageFragment1.newInstance());
        fragments.add(ScreenSlidePageFragment2.newInstance());
        fragments.add(ScreenSlidePageFragment3.newInstance());

        return fragments;
    }


    public void onClickBtn_pular(View view) {

        mPager.setCurrentItem(mPager.getCurrentItem() + 1);

    }

    public void onClickVoltar(View view){
        onBackPressed();
    }
    private void compartilha(){

    }

    private void gravaItems(){

    }

    @Override
    public void onBackPressed() {
        if (mPager.getCurrentItem() == 0) {
            // If the user is currently looking at the first step, allow the system to handle the
            // Back button. This calls finish() on this activity and pops the back stack.
            super.onBackPressed();
        } else {
            // Otherwise, select the previous step.
            mPager.setCurrentItem(mPager.getCurrentItem() - 1);
        }
    }
}

Here’s my Ragment, the first on the list:

public class ScreenSlidePageFragment1 extends Fragment {

    private GravaItems gravaItems;
    private Button btn;

    public static ScreenSlidePageFragment1 newInstance(){

        return new ScreenSlidePageFragment1();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        ViewGroup rootView = (ViewGroup) inflater.inflate(
                R.layout.fragment_screen_slide_page1, container, false);

        return rootView;
    }

}

its layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/appCompatImageView1"
        android:layout_width="120dp"
        android:layout_height="0dp"
        android:layout_marginTop="37dp"
        android:layout_marginBottom="28dp"
        app:layout_constraintBottom_toTopOf="@+id/textView19"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/logo2" />

    <TextView
        android:id="@+id/textNome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="228dp"
        android:text="Nome:"
        android:textColor="@color/verde_escuro"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="@+id/edtNome"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/edtNome"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="9dp"
        android:layout_marginEnd="20dp"
        android:hint="Digite seu nome"
        android:padding="5dp"
        android:textColor="@color/cinza"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textNome" />

    <TextView
        android:id="@+id/textTelefone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Telefone:"
        android:textColor="@color/verde_escuro"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="@+id/edtNome"
        app:layout_constraintTop_toBottomOf="@+id/edtNome" />

    <EditText
        android:id="@+id/edttelefone"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="25dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="25dp"
        android:hint="(31) 91234-1234"
        android:inputType="phone"
        android:padding="5dp"
        android:textColor="@color/cinza"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textTelefone" />

</androidx.constraintlayout.widget.ConstraintLayout>

And here, my viewpager2:

public class ScreenSlidePagerAdapter extends FragmentStateAdapter {

    private ArrayList<Fragment> fragments = new ArrayList<>();
    private FragmentActivity fa;

    public ScreenSlidePagerAdapter(FragmentActivity fa, ArrayList<Fragment> fragments) {
        super(fa);
        this.fa = fa;
        this.fragments = fragments;

    }


    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return fragments.get(position);
    }

    @Override
    public int getItemCount() {
        return fragments.size();
    }

How to get to the next Ragment and have the edittexts ,recorded or manipulated, in this action? Thank you

  • There are several alternatives. One of them is accessing a method (public) of your desired fragment through the ScreenSlideActivity. Another is storing in Local Storage, which doesn’t seem to make sense in your case if I got it right. You can even use a static class for this, but I don’t think it’s a good idea.

  • solved with sharedpreferences, found faster, if you want to answer better, mark as response

No answers

Browser other questions tagged

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