InhoLee
InhoLee

Reputation: 59

How to pass data from an Activity to a Fragment?

There's a lot of questions about pass Data Activity to Fragment.

But I have a problem with "replace" method. I use fragment because of sliding tablayout.

I want to pass data from Character1_activity.java -> TabFragment1.java

Here's the code.

Character1_Activity.java

public class Character1_Activity extends Activity implements View.OnClickListener {
    public String food_box;
    public String water_box;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        layoutParams.dimAmount = 0.7f; // 흐린 정도
        getWindow().setAttributes(layoutParams);
        setContentView(R.layout.character1);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.closebutton1:
                Intent intent = new Intent(this, Next_day.class);
                CheckBox food = (CheckBox) findViewById(R.id.food_cb);
                CheckBox water = (CheckBox) findViewById(R.id.water_cb);

                if (food.isChecked() == true)
                    food_box = "true";
                else
                    food_box = "false";

                if (water.isChecked() == true)
                    water_box = "true";
                else
                    water_box = "false";

                TabFragment1 fragment = new TabFragment1();
                if (fragment != null) {
                    FragmentManager fragmentManager = getFragmentManager();
                    Bundle bundle = new Bundle();
                    bundle.putString("food_box", food_box);
                    fragment.setArguments(bundle);
                    fragmentManager.beginTransaction().replace(R.id.game_main, fragment).addToBackStack(null).commit();
                }
                this.finish();
                break;

And this is TabFragment1

public class TabFragment1 extends Fragment {
    String ch1_water;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab_fragment_1, container, false);
        getArguments().getString("water_box");
    }

Upvotes: 0

Views: 92

Answers (2)

JBJ
JBJ

Reputation: 288

(Considering you don't want to use bundles to pass data)You can try passing data between Activity and fragment by making a common singleton class. Depending on your requirements you can create a class to set and get data. Below is one example of a singleton class which can be used to pass data between fragments

public class DataClass {
    private static DataClass dataObject = null;

    private DataClass() {
    }

    public static DataClass getInstance() {
        if (dataObject == null)
            dataObject = new DataClass();
        return dataObject;
    }

    private String distributor_id;;

    public String getDistributor_id() {
        return distributor_id;
    }

    public void setDistributor_id(String distributor_id) {
        this.distributor_id = distributor_id;
    }
}

To set data

DataHolderClass.getInstance().setDistributor_id("your data");

and to get data

String data = DataClass.getInstance().getDistributor_id();

Upvotes: 0

Navjot.jassal
Navjot.jassal

Reputation: 779

From Activity:-

Bundle bundle = new Bundle();
bundle.putString("food_box", foodbox);
fragment.setArguments(bundle);

Fragment onCreateView method:

    String strtext = getArguments().getString("food_box");    

Upvotes: 2

Related Questions