1

So, hopefully this question is not vaguely described, my scenario is the following:

In my app I have three flows (evidently could be more in the future, but I have to deliver this app asap to meet the requirements and as usual make my best to have an strong foundation for the guy that come next to me) so, in those three flows there are four steps for each one and each step is fairly simple, just take one piece of data and pass it along until the end of the flow.

So, my question is: which would be the "best" approach in terms of easy to implement and scalable to be able to define those flows reusing some of the activities. In other words, I have something like:

Flow 1:

A to B to C to D display all gathered data

Flow 2:

A to E to C to D display all gathered data

Flow 3:

A to F to G to D display all gathered data

How should be the handling of the data and how I can say to each activity which is the next one after itself, besides I'm using an action bar which is going to have a back arrow so in any case you can go back to the previous step and edit the data that you had choose or created before.

Maybe the word "best" is not that suitable since I'm not asking for simple opinions instead I'm asking for the suggested method to do this in an Android app, since it is a situation that I would say many apps have to deal with.

Update I:

First round implementing this flow I got working the following approach, (kind of broken pseudo java code to be brief):

// ActionBarActivity suppose to offer me the change to deal with actionBar and fragment otherwise just Fragment
Flow1Activity extends ActionBarActivity implements AFragmentListener
    onCreate {
        newFragment = new FragmentA();
        generateTransactionTo(newFragment);
    }
    generateTransactionTo(android.app.Fragment newFragment) {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        transaction.replace(R.id.container, newFragment);
        transaction.addToBackStack(null);

        transaction.commit();
    }
    onAfragmentConfirmed() {
        // do whatever...
    }

Afterwards, each of the fragments has their own inner interface that the activity holder should implement (perhaps on each next into the fragment you will call to this injected interface) like:

FragmentA extends Fragment
    public interface AFragmentListener {
        void onAFragmentConfirmed();
    }
    onAttach(Context context) {
        super.onAttach(context);
        Activity activity = getActivity();

        try {
            mAFragmentListener = (AFragmentListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement AFragmentListener");
        }
    }
    onClickSomething() {
        mAFragmentListener.onAfragmentConfirmed()
    }

My current issue is that the ActionBar is not working as I would expected, I am setting my action bar doing like:

ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setElevation(0);
    }

But the button back is shown ant it doesn't do anything, and if I hit back on the android button below it goes to the prev activity :(

Useful links:

Community
  • 1
  • 1
Alexis Duran
  • 620
  • 14
  • 28

1 Answers1

2

One option would be to implement the screens as Fragments .

Each fragment has a callback to the activity (e.g. showNext). Then you can implement an Activity for each Flow.

E.G. Activity 1 shows fragments A -> B -> C -> D Activity 2 shows fragments A -> E -> C -> D Activity 3 shows fragments A -> F -> G -> D

This is clean and scaleable because it separates concerns.

Fragments: show content on screen Activities: navigate between screens

cyroxis
  • 3,661
  • 22
  • 37
  • So when a fragment call "show next" the wrapper activity knows who to call? And what happens with the back button once the new fragment is shown and the previous is remove from the current view the back button has an stack reference of what happens? (Sorry but I want to double check that I understand the approach) – Alexis Duran Apr 22 '16 at 15:11
  • When replacing the fragment, just add it to the fragment back stack so back navigation is supported. – cyroxis Apr 22 '16 at 15:36