0

I am trying to pass a value from my Activity to my Fragment but the Bundle is always null.

Activity

CallLogsFragment callLogfrag = new CallLogsFragment();
Intent intent = new Intent(this, DeviceUsageActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("key", callgroup);
callLogfrag.setArguments(bundle);
this.startActivity(intent);

Fragment which is meant to retrieve value from Activity

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    bundle = this.getArguments();
    if (bundle != null) {
        int myInt = bundle.getInt("key", 0);
    }
    View view = inflater.inflate(R.layout.fragment_call_logs, container, false);
    load(view);
    setupList(view);

    return view;
}
vidulaJ
  • 1,232
  • 1
  • 16
  • 31
Zidane
  • 1,696
  • 3
  • 21
  • 35
  • Then obviously onCreateView is not called inside the fragment which he setup here (maybe the one from activities's layout?) – Selvin Oct 28 '16 at 10:57
  • DeviceUsageActivity.class is activity or fragment – Umar Ata Oct 28 '16 at 11:11
  • @zidane can you please respond, if you are starting an activity then you should put intent extras and get it inside your activity by using getIntent().getExtras.get(key) – Umar Ata Oct 28 '16 at 11:21
  • Why are you doing `this.startActivity(intent)`? That starts a new Activity. Don't you want to attach a Fragment? – denvercoder9 Oct 28 '16 at 11:29
  • Possible duplicate of [Send data from activity to fragment in android](http://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android) – denvercoder9 Oct 28 '16 at 11:33

3 Answers3

1

try this one

 // send value from activity to fragment 

CallLogsFragment callLogfrag = new CallLogsFragment();
Bundle bundle = new Bundle();
bundle.putInt("key", "4");
callLogfrag.setArguments(bundle);
 // call fragment from activity
getSupportFragmentManager().beginTransaction().replace(R.id.containar, callLogfrag).commit();

//  get value in fragment 

    if (getArguments() != null) {
        int status = getArguments().getInt("key");
anu
  • 213
  • 1
  • 3
  • 10
0

If I am clear with your question!! You are trying to send data from one activity to the fragment in Another activity.

Then you need to first send data in Intent that starts second activity. Then get the data from intent and set your data to fragment as argument and add the fragment to the parent activity.

//here you go, from first activity
Intent intent = new Intent(this, DeviceUsageActivity.class);
 Bundle bundle = new Bundle();
bundle.putInt("key", callgroup);
intent.putExtra("yourdata",bundle);
//start your activity - parent activity of your fragment
this.startActivity(intent);

//then get your data in DeviceUsageActivity in onCreate()
 Bundle yourdata = null;
if(getIntent().getExtras() != null) {
       yourdata = getIntent().getBundleExtra("yourdata");
}

//Then sent data to fragment
CallLogsFragment callLogfrag = new CallLogsFragment();

callLogfrag.setArguments(yourdata);
 // add fragment to parent activity
getSupportFragmentManager().beginTransaction().replace(R.id.containar, callLogfrag).commit();

//rest you know - get bundle argument in fragmet
Babasaheb
  • 683
  • 6
  • 20
-1

From Activity to store data

Intent i = new Intent(getApplicationContext(), NewActivity.class);

    i.putExtra("key","value");
    startActivity(i);

To retrieve data back to fragment,use Context as getActivity if needed only in fragments

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("key");
    //The key argument here must match that used in the other activity
}