Data from One Activity to Fragment

We Know how to send the data from the Activity to another Activity. But what if we want to send the data from Activity to fragment?

So the technique is very similar to send data to activity. For sending the data to fragment we use the Bundle.

Bundles: A mapping from String keys to various Parcelable values. They are generally used for passing data between various Android activities and fragments.


All fragments should have an empty constructor (i.e. a constructor method having no input arguments). Therefore, in order to pass your data to the Fragment being created, you should use the setArguments() method. This methods gets a bundle, which you store your data in, and stores the Bundle in the arguments. Subsequently, this Bundle can then be retrieved in onCreate() and onCreateView() call backs of the Fragment.

Sending Data from Activity
DataFragment fragment = DataFragment.newInstance();
Bundle bundle = new Bundle();
bundle.putString("name", name);
bundle.putInt("age", age);
bundle.putBoolean("gender", gender[0]);
fragment.setArguments(bundle);

Receiving Data in Fragment
Bundle bundle = getArguments();
String name = bundle.getString("name");
int age = bundle.getInt("age");
boolean gender = bundle.getBoolean("gender");

MainActivity.java
package com.theitbulls.activitytofragmentdatademo;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;

import com.theitbulls.activitytofragmentdatademo.ui.main.DataFragment;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

    }

    public void showData(View view) {
        AppCompatEditText etName = findViewById(R.id.etName);
        AppCompatEditText etAge = findViewById(R.id.etAge);

        final boolean[] gender = {false};
        String name = etName.getText().toString();
        int age = Integer.parseInt(etAge.getText().toString());

        RadioGroup radioGroup = findViewById(R.id.rbg);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                int id = radioGroup.getCheckedRadioButtonId();
                gender[0] = id == R.id.rbMale;
            }
        });

        DataFragment fragment = DataFragment.newInstance();
        Bundle bundle = new Bundle();
        bundle.putString("name", name);
        bundle.putInt("age", age);
        bundle.putBoolean("gender", gender[0]);
        fragment.setArguments(bundle);

        getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commitNow();
    }
}


DataFragment.java
package com.theitbulls.activitytofragmentdatademo.ui.main;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.fragment.app.Fragment;

import com.theitbulls.activitytofragmentdatademo.R;

public class DataFragment extends Fragment {
    public static DataFragment newInstance() {
        return new DataFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment, container, false);

        Bundle bundle = getArguments();
        String name = bundle.getString("name");
        int age = bundle.getInt("age");
        boolean gender = bundle.getBoolean("gender");

        AppCompatTextView tvName = root.findViewById(R.id.tvName);
        AppCompatTextView tvAge = root.findViewById(R.id.tvAge);
        AppCompatTextView tvGender = root.findViewById(R.id.tvGender);

        tvName.setText(name);
        tvAge.setText(String.valueOf(age));
        tvGender.setText(gender ? "Male" : "Female");

        return root;
    }
}

Download: ActivityToFragmentDataDemo.zip