I'm trying to send a Parcelable array from MainActivity to one of its fragment using bundle. My code is
Bundle bundle = new Bundle();
MyFragment fragment = new MyFragment();
bundle.putParcelableArray(key, MyParcelableArray);
fragment.setArguments(bundle);
When debugged, I found that MyParcelableArray is not null but bundle mParcelledData is null. Thus, it is throwing a null pointer error.
Why is this happening?
I'm retrieving code in the fragments onCreateView as below.
Bundle bundle = getArguments();
Parcelable[] parcelables = bundle.getParcelableArray(MainActivity.key);
getting null pointer in the above line.
While opening a fragment use below code
Bundle bundle= new Bundle();
bundle.putSerializable("data", MyParcelableArray);
ListFrag newFragment = new ListFrag ();
newFragment.setArguments(bundle);
While obtaining that bundle data in fragment use below code
Bundle b = getArguments();
ArrayList<collection> yourArrayList = (ArrayList<collection>) b.getSerializable("data");
Related
I create the bundle in my Activity with
ListFragment fragment = new ListFragment();
Bundle bundle = new Bundle();
bundle.putInt("i", 0);
Log.i("Bundle", String.valueOf(bundle.getInt("i")));
fragment.setArguments(bundle);
And I get the arguments in my Fragment with
Bundle bundle = this.getArguments();
if (bundle != null) {
myInt = bundle.getInt("i", -1);
}
But it says that my bundle is null. Any idea why?
Are you sure that the fragment you want to read the arguments was created from the provided code block #1? Your code is correct, there is nothing wrong so it must work. (as long as the arguments are accessed after onCreate, which they are)
I had the same issue, I initially had my bundle variable declared outside the onCreateView function. Once I moved it inside, it worked for me
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 ?
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
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
I have a fragment with an ExpandableList which content is provided by a HashMap<String,List<String>>. This HashMap<String,List<String>> is filled in Fragment's Activity and i need to pass it to the fragment.
To do it, i was planning to do a Fragment transaction but i found the problem:
In the method "newInstance" of the fragment, when setting arguments to the Bundle, there is no method to put a HashMap.
Ex:
Bundle args = new Bundle();
args.putInt(ARG_PARAM1,index); //method .putHashmap doesn't exist.
So, how can i pass my hashmap in bundle to the Fragment? Please provide an solution for this one.Thanks in advance
To pass HashMap do this
MyFragment fr = new MyFragment(); // Replace with your Fragment class
Bundle bundle = new Bundle();
bundle.putSerializable("hashmap",mMap);
fr.setArguments(bundle);
To retrieve do this
HashMap<String,List<String>> mMap = new HashMap<String,List<String>>();
Bundle b = this.getArguments();
if(b.getSerializable("hashmap") != null)
mMap = (HashMap<String,List<String>>)b.getSerializable("hashmap");
Try using:
Bundle args = new Bundle();
args.putSerializable(ARG_PARAM1, hereGoesYourHashMap);
as according to the docs:
https://developer.android.com/reference/java/util/HashMap.html
HashMap is Serializable
I have a activity and a fragment activity . I want to fetch email id stored in my activity to fragment activity . Can anyone of you tell me how to pass data from activity to fragments . I used bundle but it is not working
Use this code in your activity:
Bundle args = new Bundle();
Fragment frag=new Frag1();
args.putInt("key", value);
frag.setArguments(args);
And now in your fragment class, get the bundle as:
Bundle args = getArguments();
if(args.containsKey("key"))
{
args.getInt("key");
}
Here is my code that is not working:
// Sending bundle this way:
String topUser = String.valueOf(scores.get(arg2));
Bundle data = new Bundle();
data.putString("userprofile", topUser);
FragmentTransaction t = getActivity().getSupportFragmentManager()
.beginTransaction();
SherlockListFragment mFrag = new ProfileFragment();
mFrag.setArguments(data);
t.replace(R.id.main_frag, mFrag);
t.commit();
// Retrieving this way:
Bundle extras = getActivity().getIntent().getExtras();
userName = extras.getString("userprofile");
Basically, the data isn't received. Am I on the right track or is there a better way of doing this?
You should be using the getArguments() method of the Fragment class. So put something like the following inside your Fragment:
Bundle extras = getArguments();
Reference: http://developer.android.com/reference/android/app/Fragment.html#getArguments()