Android: Pass Data(Extras) to a Fragment

Android: Pass data(extras) to a fragment

Two things. First I don't think you are adding the data that you want to pass to the fragment correctly. What you need to pass to the fragment is a bundle, not an intent. For example if I wanted send an int value to a fragment I would create a bundle, put the int into that bundle, and then set that bundle as an argument to be used when the fragment was created.

Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);

Second to retrieve that information you need to get the arguments sent to the fragment. You then extract the value based on the key you identified it with. For example in your fragment:

Bundle bundle = this.getArguments();
if (bundle != null) {
int i = bundle.getInt(key, defaulValue);
}

What you are getting changes depending on what you put. Also the default value is usually null but does not need to be. It depends on if you set a default value for that argument.

Lastly I do not think you can do this in onCreateView. I think you must retrieve this data within your fragment's onActivityCreated method. My reasoning is as follows. onActivityCreated runs after the underlying activity has finished its own onCreate method. If you are placing the information you wish to retrieve within the bundle durring your activity's onCreate method, it will not exist during your fragment's onCreateView. Try using this in onActivityCreated and just update your ListView contents later.

How to pass intent extras to multiple fragment inside Activity viewpager from PreviousActivity

Here is solution, you have to create 2 methods in Activity A

1. passIntent1()
private void passIntent1(){
Intent intent = new Intent(getBaseContext(), SecondActivity.class);
intent.putExtra("INTENT_VALUE", "A");
intent.putExtra("FRAGMENT_A", value for A);
startActivity(intent);
}

2. passIntent2()
private void passIntent2(){
Intent intent = new Intent(getBaseContext(), SecondActivity.class);
intent.putExtra("INTENT_VALUE", "B");
intent.putExtra("FRAGMENT_B", value for B);
startActivity(intent);
}

Now, Inside B Activity, get your Intent

    String value = getIntent().getStringExtra("INTENT_VALUE");
if(value.equalsIgnoreCase("A")){
String FragA = getIntent().getStringExtra("FRAGMENT_A");
}else if(value.equalsIgnoreCase("B")){
String FragB = getIntent().getStringExtra("FRAGMENT_B");
}

Now pass the value FragA to Fragment A and FragB to Fragment B

UPDATED CODE BELOW

In Activity use below code to pass Values in fragments.
You have to use Bundle to pass data to fragments

Home home = new Home();
Bundle homeBundle = new Bundle();
homeBundle.putString("KEY","VALUE");
// Your Key must same when you get your values in Fragment Home
home.setArguments(homeBundle);

Similarly you have do same for Fragment Grade

Grade grade = new Grade();
Bundle gradeBundle = new Bundle();
gradeBundle.putString("KEY","VALUE");
grade.setArguments(gradeBundle);

Add fragments to adapter

adapter.addFragment(home, "Fragment I");
adapter.addFragment(grade, "Fragment II");

Now in side Fragment, to get values use below code

Bundle bundle = getArguments();
if(bundle!=null){
String value = bundle.getString("KEY");
}

Hope, now you understand in better way.

Send data from activity to fragment in Android

From Activity you send data with intent as:

Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);

and in Fragment onCreateView method:

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

android passing and retrieving extra from activity to fragment

You need to use the setArguments() method of Fragment to pass information into your fragment. In the activity that creates the fragment, do something like this:

YourFragment f = new YourFragment();
Bundle args = new Bundle();
args.putString("friendIDRowID", getIntent().getExtras().getString("friendIDRowID"));
f.setArguments(args);
transaction.add(R.id.fragment_container, f, "tag").commit();

Then, override the onCreate() method of your Fragment and do the following:

Bundle args = getArguments();
String myString = args.getString("friendIdRowID");

Like extras for an Activity, you can add as many things to your arguments bundle as you wish. Hope this helps!

putExtra from activity then getExtra in fragment

You can't create a Fragment with startActivity. You need to create the fragment with bundle like this:

ProfileFragment fragment = new ProfileFragment();
Bundle args = new Bundle();

args.putString("name", users.name1);
args.putString("family", users.family1);
fragment.setArguments(args);

// then tell the FragmentManager to attach the fragment
// to the activity
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.your_placeholder, fragment);
ft.commit();

Then in your onCreateView get them with:

String name = getArguments().getString("name", ""); 
String family = getArguments().getString("family", "");

Please remember that you need to move the return code to the last of onCreateView method and change your code to something like this:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile2, container, false);

TextView pn =view.findViewById(R.id.pn);
...

return view;
}


Related Topics



Leave a reply



Submit