1

I am trying to build a delivery app. I have a list of products to choose from. After the user selects a product then he is lead to a series of stages to define extras and options of a certain product.

List of products:

The list of products

Then, Lets say someone clicks on one of the products, we go to the controller FragmentActivity:

enter image description here

The subtotal at top and the button at the bottom of the page belongs to the fragment activity. Then I place a group of radio buttons at the center layout. Everything is fine till now. Click on the button leads for the replacement of the fragment:

enter image description here

Everything is beautiful until now. I can access the buttons and the subtotal through the fragments. However, if I press the back button on the device it leads me back to the list of products and not to the previous fragment. Even if I manage to go back to the previous fragment it would lose the radiobutton selection as well.

Then the next fragment is a calculation of the products and its extras:

enter image description here

When I press the button, I just use finish() in the fragment and it leads me back to the list of products which is my desired result. However, I need to know that I am coming from there in the list of products so I can add that product to the shopping cart that is being built for the delivery order.

I am really new in using fragments, but I can pass arguments just fine. What I am struggling is to control the navigation of the fragments through the FragmentActivity that controls the fragments. Also, I am struggling to keep the states of the fragments (remembering user input). At last, I need to go back to the list of products with a result of that item that was being constructed so I can add it to the shopping cart.

Am I going to the right direction? How can I implement these features(navigation, fragment state, returning to previous activity with some data since I just use finish()), Many thanks guys!

Gustavo Baiocchi Costa
  • 1,379
  • 3
  • 16
  • 34

1 Answers1

2

You can navigate between the fragments just by adding them into the back stack like following:

// Works with either the framework FragmentManager or the
// support package FragmentManager (getSupportFragmentManager).
getSupportFragmentManager().beginTransaction()
                           .add(detailFragment, "detail")
                           // Add this transaction to the back stack
                           .addToBackStack()
                           .commit();

That way, when you will click the back button, it wont load the previous activity from the stack but the previous fragment that has been added in the backstack. You can find more details here: http://developer.android.com/training/implementing-navigation/temporal.html

Use the onSaveInstanceState(Bundle savedInstanceState) and the onActivityCreated(Bundle savedInstanceState) on every fragment that you want to save and retrieve data. And then do the following:

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (savedInstanceState != null) {
        //Restore the fragment's state here
        String yourString  = savedInstanceState.getString("key");
    }
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    //save whatever you want into the bundle
    savedInstanceState.putString("key", "your_value");
    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

You can save whatever you want into the Bundle. From Strings to Parcelables and Serializables. More info here: http://developer.android.com/reference/android/os/Bundle.html