-3

I want to pass data from MainActivity to Fragment i'm using this for my action

switch (txt.getText().toString()){

       case "Jumlah(Tinggi-Rendah)":
         dialog.dismiss();
         cond = "desc";
         by = "jumlah";
         Log.e("By1 : ", ""+by );
         Intent intent = new Intent("KEY");
         sendBroadcast(intent);
         return;

and this is my get String

public String getDataAkun(){

   return idakun;
}

public String getDataBy(){
    return by;
}

and this is my script in my fragment

MainActivity activity = (MainActivity) getActivity();

    idakun = activity.getDataAkun();
    by = activity.getDataBy();

this is my broadcast

private BroadcastReceiver updateProfileBroadcast = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        //Fire your event
        list_transaksi();
        Toast.makeText(getContext(),"huft :"+by+" - "+idakun,Toast.LENGTH_LONG).show();
    }
};

when i run my action just "idakun" who get the data but "by" is returning null

enter image description here im new on android programming so if there any help please ...

jgm
  • 1,230
  • 1
  • 19
  • 39
matin
  • 11
  • 2

1 Answers1

1

To send data from activity to fragment simply create a Bundle and put required params in that and set that bundle in fragment.setArguments() when you are attaching or replacing that fragment.

Here is an example:

Bundle bundle = new Bundle();
bundle.putInt("Key", 2);
Fragment frag = new YourFragment();
frag.setArguments(bundle);
--> then add or replace fragment as you want.

Update: in the onCreate () method of your fragment put below code:

Bundle bundle = getArguments (); int i = bundle.getInt ("key");

Still not clear then comment below.

Sangeeta
  • 961
  • 2
  • 13
  • 34