top of page
  • Writer's pictureMeghan Gill

Bundles: Passing Info to a Fragment

Updated: Sep 10, 2021




What is a Bundle?


A bundle is a way to store key/value pairs. Decide what type of data your are sending in the bundle. Some examples of data types you can send are a String, char, boolean, int, byte, booleanArray, intArray, etc.


You will need a String to hold the key and a variable of the correct data type to store the value.



When and where are bundles used?


Bundles are generally used for passing data between various Android activities and/or fragments. It depends on you what type of values you want to pass, but bundles can hold all types of values and pass them to the new activity.

Why do we need bundles?

Stackoverflow has an excellent explanation. When the screen rotates, or when another activity is started, the method protected void onSaveInstanceState(Bundle outState) is invoked, and the activity is destroyed. Later, another instance of the activity is created, and public void onCreate(Bundle savedInstanceState) is called. When the first instance of the activity is created, the bundle is null; and if the bundle is not null, the activity continues some business started by its predecessor.

Android automatically saves the text in text fields, but it does not save everything, and subtle bugs sometimes appear. The most common anti-pattern, though, is assuming that onCreate() does just initialization. It is wrong, because it also must restore the state.

There is an option to disable this "re-create activity on rotation" behavior, but it will not prevent restart-related bugs, it will just make them more difficult to detect.



Steps for Sending a Bundle to a Fragment

  1. The bundle will be saved using a key/value pair, so in MainActivity.java create a String variable for the key.

  2. Create a new bundle.

  3. Use the appropriate method from the Bundle class to send your bundle. What data type does your bundle contain? Choose the appropriate method and send a key/value pair.

  4. Here are some possibilities: Use putInt(), putBoolean(), putString(), putChar(), putByte(), putBooleanArray(), etc.

  5. Create a new instance of the fragment to which you would like to send the bundle.

  6. Use the setArguments() method to send the bundle to the fragment.


MainActivity.java:

public static final String KEY_IS_BLUE_BTN_PICKED = "is_blue_button_picked";

private void sendBundle() {
    Bundle bundle = new Bundle();
    bundle.putBoolean(KEY_IS_BLUE_BTN_PICKED, blueBtnIsPicked);
    BlueFragment blueFragment = new BlueFragment();
    blueFragment.setArguments(bundle);
    
}

Steps for Retrieving a Bundle in a Fragment

1. In your fragment create a String variable to hold the key for the bundle key/value pair. This should be the same key used in MainActivity.java.

2. In the method public View onCreateView() create a variable to hold the value from the bundle, i.e. I sent a boolean, so my variable should be a boolean.

  1. To retrieve the value of the bundle, call getArguments().

  2. Then via method chaining call the method appropriate for your data type, i.e. getBoolean(), getString(), etc.

  3. Enter your key.


BlueFragment.java


public static final String KEY_IS_BLUE_BTN_PICKED = "is_blue_button_picked";

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    boolean blueBtnChosen =     
        getArguments().getBoolean(KEY_IS_BLUE_BTN_PICKED);
    
 //If you'd like, display the value in a toast    
    Toast.makeText(getActivity(), "Blue Button Chosen? " +         
        blueBtnChosen, 
        Toast.LENGTH_SHORT).show();
        
 //Inflate your layout 
    View view = inflater.inflate(R.layout.fragment_blue, container,     
        false);
    return view;
}



7,455 views0 comments

Recent Posts

See All
bottom of page