Android (Java) Activity that chooses which fragment to use

Derek :

I am trying to figure out a way to have my Activity decide which fragment to choose. What I have is a list of fragments and when you click on any of them they all display the same thing. I want to be able to display my 3 different fragments I have starting with one and going to the last, basically just repeating the 3 over and over.

My activity code looks like this

package com.example.ddursteler1.workouttracker;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;

import java.util.List;
import java.util.UUID;

public class WorkoutActivity extends SingleFragmentActivity {

    public static final String EXTRA_WORKOUT_ID = "com.example.ddursteler1.workouttracker.workout_id";

    private static UUID mUUID;


    public static Intent newIntent(Context packageContext, UUID workoutId) {
        Intent intent = new Intent(packageContext, WorkoutActivity.class);
        intent.putExtra(EXTRA_WORKOUT_ID, workoutId);
        return intent;
    }

    @Override
    protected Fragment createFragment() {
        UUID workoutId = (UUID) getIntent().getSerializableExtra(EXTRA_WORKOUT_ID);

        Log.d("createFragment UUID", workoutId.toString());
        return WorkoutFragment1.newInstance(workoutId);
    }
}

I want WorkoutFragment1 to be the first item in the fragment list, WorkoutFragment2 the second, WorkoutFragment3 the third and then repeat.

I have been following The Big Nerd Ranch programming book, so it is very similar to the CriminalIntent app. Any help is appreciated.

Beyazid :

You are opening only WorkoutFragment1. You can create other fragments. And you should check in your activity like this.

    UUID workoutId = (UUID) 
    getIntent().getSerializableExtra(EXTRA_WORKOUT_ID);

    Log.d("createFragment UUID", workoutId.toString());
    if(workoutId==idOfWorkoutFragment1){
       return WorkoutFragment1.newInstance();
    }else if(...){
    ....
    ....
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=159044&siteId=1