0

I want to receive data from class to fragment by Intent ,I try to do ,I write

Intent n = this.{getIntent()};

{the wrong here} ,but this code is not working ,so what i do ?

Fredrick Gauss
  • 5,126
  • 1
  • 28
  • 44
  • Are you passing some data from an activity to a fragment or from one fragment to other fragment? you can put your code here,it will be much easier for us to understand. – Ritt Sep 20 '15 at 07:02
  • Actually, it depend on who data is sending from sender. in normal case everyone use `setArgument()` which take Bundle object in sender and in Fragment onCreateView methos getArgument() method is used to retrieve Bundle – ρяσѕρєя K Sep 20 '15 at 07:02

2 Answers2

0

Activity Send Intent data to fragment

Bundle b = new Bundle();
b.putString("data", "abc");
// set Fragmentclass Arguments
Fragment frag = new Fragment();
frag.setArguments(b);

Fragment receive data with intent

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    String data = getArguments().getString("data");    
    return inflater.inflate(R.layout.fragment, container, false);
}

For fragments, you can visit Send data from activity to fragment in android

Community
  • 1
  • 1
Nooruddin Lakhani
  • 7,507
  • 2
  • 19
  • 39
  • That what i did it at class: Intent k = new Intent(Add.this,MM.class); k.putExtra("namemeal", nam); startActivity(k); now i want receive it at fragment – m.developer Sep 21 '15 at 05:12
  • Receiver fragment is the main activity at my app , when you lunch app ,app show fragment 1(receiver) , then you will go to fragment 2 for going to class and sending data to class by Intent ,until now app work well ,in this class you press button for going to fragment1 and sending data to fragment 1 ,and show this data . – m.developer Sep 21 '15 at 07:47
  • i try your code , but when i lunch my app , the app stop and get out – m.developer Sep 22 '15 at 06:56
0

Try this,

you can send data from activity using,

Bundle bundle = new Bundle();
bundle.putString("key", "value");
Fragmentclass fc= new Fragmentclass();
fc.setArguments(bundle);

and receive from this way in your fragment's onCreateView method,

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

that's it

Ganpat Kaliya
  • 888
  • 2
  • 9
  • 16