-1

I have passed some key and value data from one activity to another activity fragment so I have not get key and value to the last point in the fragment. I have passing data using bundle.

4 Answers4

2

In your activity create bundle to set to the fragment

Yourfragment fragment = new Yourfragment();
Bundle args = new Bundle();
args.putString(ARG_DATA, data);
fragment.setArguments(args);

Then load the fragment getSupportFragmentManager().beginTransaction().replace(R.id.your_container,fragment).commit();

Then in your fragment oncreate get the data like this

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mData = getArguments().getString(ARG_DATA);
    }
}
Sharath kumar
  • 4,064
  • 1
  • 14
  • 20
  • stop farming reputation you should close this question as duplicate don't answer duplicate question **its a just suggestion** – Goku Nov 10 '17 at 05:33
2

From activity to activity you can pass data by using Intent and when you get data in second activity in which you are creating fragments on creating fragment pass that data to fragment by making constructor in fragment or by using bundle. For further assistance and if you dont know how to do this do let me know.

Muhammad Saad Rafique
  • 3,158
  • 1
  • 13
  • 21
2

Pass data from activity to activity:

Intent intent = new Intent(this, Second.class);
intent.putExtra("data", sessionId);
startActivity(intent);

get data in Second activity:

String s = getIntent().getStringExtra("data");

Pass data from activity to fragment:

Bundle bundle = new Bundle();
bundle.putString("data", "From Activity");
// set Fragmentclass Arguments
FragmentOne fragment = new FragmentOne ();
fragment.setArguments(bundle);

get data from activity to fragment:

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

}

Happy coding!!

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
1

You can pass data between Activity and Fragments and Fragment to fragment using below:

https://developer.android.com/training/basics/fragments/communicating.html

But to pass it between activity, you may use bundle data, (very few variables) or use Application Class to store it in memory, make sure you do not bloat up your memory.

New Android architecture components also do provide good options, it all depends upon your use:

https://developer.android.com/topic/libraries/architecture/index.html

halfer
  • 19,824
  • 17
  • 99
  • 186
Niraj Sanghani
  • 1,493
  • 15
  • 23