I am using the example give in the below link
http://android-er.blogspot.in/2013/04/handle-onlistitemclick-of-listfragment.html
Here i have two classes one extending List Fragment and other extending Fragment. Now i am passing object to detailfragment in this way :
*from ListFragment *
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Detailfragment detailFragment = (Detailfragment)getFragmentManager().findFragmentById(detailFragmentID);
Bundle bundle = new Bundle();
bundle.putSerializable(BUNDLE_KEY, obj);// passing this object
detailFragment.setArguments(bundle);
detailFragment.setUpLayout();// update the UI
}
Now in the Fragment class i receive it,basic goal is to update the UI of the fragment based on the item selected in the list fragment, thats the reason i am sending the object
Bundle b = getArguments();
b.getSerializable(BUNDLE_KEY);
Now on item selected it says "Fragment already active".
What is the issue here? what am i doing wrong?
Another solution is to create an empty constructor for your fragment.
public Detailfragment() {
super();
// Just to be an empty Bundle. You can use this later with getArguments().set...
setArguments(new Bundle());
}
and in the onListItemClick method you use that bundle:
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Detailfragment detailFragment = (Detailfragment)getFragmentManager().findFragmentById(detailFragmentID);
// Update the keys.
detailFragment.getArguments().putSerializable(BUNDLE_KEY, obj);// passing this object
detailFragment.setUpLayout();// update the UI
}
Now you can call the getArguments() methond in your setUpLayout() method.
From the Official Android development Reference:
public void setArguments (Bundle args) Supply the construction arguments for this fragment. This can only be called before the fragment has been attached to its activity; that is, you should call it immediately after constructing the fragment. The arguments supplied here will be retained across fragment destroy and creation.
Your fragment is already attached to its activity i suggest you to use your own method, you don't need setArguments!
create your own setUIArguments(Bundle args) inside the fragment class and update the fragment UI inside this method
You will call this method in this way:
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Detailfragment detailFragment = (Detailfragment)getFragmentManager().findFragmentById(detailFragmentID);
Bundle bundle = new Bundle();
bundle.putSerializable(BUNDLE_KEY, obj);// passing this object
detailFragment.setUIArguments(bundle); /* your new method */
}
in your fragment class
public void setUIArguments(Bundle args) {
getActivity().runOnUiThread(new Runnable() {
public void run() {
/* do your UI stuffs */
}
}
}
You can check if there are already arguments, and if so just add/update them.
private static void initFrag(Fragment frag, Bundle args) {
if (frag.getArguments() == null) {
frag.setArguments(args);
} else {
//Consider explicitly clearing arguments here
frag.getArguments().putAll(args);
}
}
Optionally, you might want to clear away existing arguments if you can't safely assume that pre-existing arguments are still valid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With