Reputation:
I am trying to pass values through a Bundle
using setArguments()
from an activity to another class extends Fragment
when I press one of the listed items in a ListView
.
As shown below, I used a Log.i
statement to know if the Bundle
received is null
or not. Unfortunately, it is always null
and consequently, no data shows on the designated TextView
of the class that extends Fragment
. What I am missing or what is wrong with the code.
FragmentClass
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragmentclasslayout, null);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
if (getArguments() != null) {
Log.i(TAG, "getArgument is not null");
int value = getArguments().getInt("pos");
TextView tv = (TextView) getView().findViewById(R.id.tvID);
tv.setTextSize(value);
tv.setText("size = "+value*10);
}else {
Log.i(TAG, "getArgument is null");
}
}
MainClass_implements_onItemSelectedListener
:
setContentView(R.layout.mainactivity);
ListView mListView = (ListView) findViewById(R.id.list);
ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(getApplicationContext(),
R.layout.listviewlayout, R.id.tv, string);
mListView.setAdapter(mArrayAdapter);
mListView.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Fragment f = new FragmentClass();
Bundle bundle = new Bundle();
bundle.putInt("pos", (position+1));
FragmentClass fc = new FragmentClass().newInstance(bundle);
//f.setArguments(bundle);
FragmentTransaction t =
getFragmentManager().beginTransaction();
t.replace(R.id.fragment00ID, f);
t.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
t.addToBackStack(null);
t.commit();
}
mainActivity_layout
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
tools:ignore="MergeRootFrame">
<ListView
android:id="@+id/list"
android:layout_width="100px"
android:layout_weight="1"
android:layout_height="match_parent" />
<fragment
android:name="com.example.fragmentwithlistview.FragmentClass"
android:id="@+id/fragment00ID"
android:layout_toRightOf="@+id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="4" />
FragmentClass_layout
:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:ignore="MergeRootFrame"
android:background="#FFFFFF">
<TextView
android:id="@+id/tvID"
android:textColor="#000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Upvotes: 2
Views: 7119
Reputation: 3827
Better use a static method for instantiating:
public class FragmentClass extends Fragment {
public static FragmentClass newInstance(Bundle b) {
FragmentClass fragment = new FragmentClass();
fragment.setArguments(b);
return fragment;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmentclasslayout, null);
if (getArguments() != null) {
Log.i(TAG, "getArgument is not null");
int value = getArguments().getInt("pos");
TextView tv = (TextView) view.findViewById(R.id.tvID);
tv.setTextSize(value);
tv.setText("size = "+value*10);
} else {
Log.i(TAG, "getArgument is null");
}
return view;
}
}
Then simply call that method and pass the arguments-bundle as a parameter:
Bundle bundle = new Bundle();
bundle.putInt("pos", (position+1));
FragmentClass f = FragmentClass.newInstance(bundle);
Also, don't use onActivityCreated
for fetching the arguments in your FragmentClass, use onCreateView
or if required onStart
or onResume
. Have a look at fragment-lifecycles: http://developer.android.com/guide/components/fragments.html#Lifecycle
Upvotes: 1
Reputation: 3193
You can use in passing Activity
Bundle bundle = new Bundle();
bundle.putString("model", strDeckNumber);
final FragmentTransaction t = getActivity().getSupportFragmentManager().beginTransaction();
Frag1 newFragment = new Frag1();
newFragment.setArguments(bundle);
t.replace(R.id.fragment_container, newFragment);
t.addToBackStack(TAG);
t.commit();
Receiving Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
strSeriesNumber = getArguments().getString("model");
}
Upvotes: 0