Navigate between fragments (ViewFlipper)

android, android-fragmentactivity, android-fragments

Solution

Let your Activity do it.

Make public methods in your `WelcomeActivity` that call `ViewFlipper.showNext()` and `ViewFlipper.showPrevious`, something like this:

public void showNextFragment() {
    mViewFlipper.showNext();
}

public void showPreviousFragment() {
    mViewFlipper.showPrevious();
}

In your fragments, you can then call the Activity's methods, like this:

WelcomeActivity parent = (WelcomeActivity) getActivity();
parent.showNextFragment();
// or parent.showPreviousFragment();

I just typed the code here and didn't try it, there might be typo, so don't just copy-paste. But I hope it's illustrating my point well.

Problem

I have an activity (android.support.v4.app.FragmentActivity) with a ViewFlipper and within this I have several fragments `src/com.package.WelcomeActivity.java` ``` package com.package; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.Menu; public class WelcomeActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.welcome, menu); return true; } } ``` `res/layout/activity_welcome.xml` ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ViewFlipper android:id="@+id/flipper" android:layout_width="fill_parent" android:layout_height="fill_parent"> <fragment android:id="@+id/flip2" class="com.package.fragment2" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <fragment android:id="@+id/flip2" class="com.package.fragment2" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <!-- ... --> <fragment android:id="@+id/flipN" class="com.package.fragmentN" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </ViewFlipper> </LinearLayout> ``` Now each fragment are composed by two mayor parts: content and actions (back, continue) when the user tab on continue action execute a function inside the fragment but i dont know how to call the `ViewFlipper.showNext()` and `ViewFlipper.showPrevious()` inside the fragments

Original source