I am at ends wit with my code. I can not get a fragment to display after it is called by another fragment (via Activity). I read the Android Developer guide and many SO's related topic on Fragment/FragmentPagerAdapter/FragmentTransactions and could not resolve my issue. This one would have been an obvious solution Is it possible to remove a fragment without replacing it by another fragment
I have an activity class that will host a fragment based on the tab selected. These fragments are dynamically created by the FragmentPagerAdapter based on user's selection of the tab. In the first tab, it creates FragmentA (ListFragment) which consists of a list of items. When a user clicks on any of the item, it should display another fragment with that item's details but to much of my dismay (and eyes burning), all it does is print out my log in logcat. Please see my code. Perhaps, I am staring at it too long and/or just don't know enough of android to see what's going on. I suspect it has to do with this line: fragTransaction.replace(R.id.curriculumParent, fragC); Now I know this would work if the 1st parameter was a fragment container in the activity's layout file was define but since it is not done statically, I don't know what to put there except for the calling FragmentB's layout file.
Thanks in advance!
MainActivity.java
public class MainActivity extends FragmentActivity implements
FragmentB.OnColorSelectedListener {
private static final String TAG = "MainActivity";
private final Handler handler = new Handler();
private PagerSlidingTabStrip tabs;
private ViewPager pager;
private MyPagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
pager = (ViewPager) findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
adapter = new MyPagerAdapter(fm);
pager.setAdapter(adapter);
final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4,
getResources().getDisplayMetrics());
pager.setPageMargin(pageMargin);
tabs.setViewPager(pager);
}
...
...
...
public class MyPagerAdapter extends FragmentPagerAdapter {
private final String[] TITLES = { "FragmentB", "2ndTabFragment", "3rdTabFragment" };
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position)
{
return TITLES[position];
}
@Override
public int getCount()
{
return TITLES.length;
}
@Override
public Fragment getItem(int position)
{
Fragment fragment = null;
switch (position) {
case 0:
fragment = Fragment.instantiate(getBaseContext(), FragmentB.class.getName());
break;
case 1:
fragment = Fragment.instantiate(getBaseContext(), SecondTabFragment.class.getName());
break;
case 2:
fragment = Fragment.instantiate(getBaseContext(), ThirdTabFragment.class.getName());
break;
}
return fragment;
}
}
@Override
public void onColorLevelSelected(int position)
{
Log.i(TAG, "Got Here!");
FragmentC fragC = new FragmentC();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction fragTransaction = fm.beginTransaction();
**fragTransaction.replace(R.id.curriculumParent, fragC);**
fragTransaction.addToBackStack(null);
fragTransaction.commit();
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip"
android:background="@drawable/background_tabs" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tabs"
tools:context=".MainActivity" />
</RelativeLayout>
FragmentB.java
public class FragmentB extends ListFragment{
OnColorSelectedListener mCallback;
private String[] colorLevel = new String[]{
"Yellow",
"Orange",
"Green",
"Blue",
"pink",
"Black"
};
private int[] colorImages = new int[]{
R.drawable.yellow,
R.drawable.orange,
R.drawable.green,
R.drawable.blue,
R.drawable.pink,
R.drawable.black
};
public interface OnColorSelectedListener{
public void onItemLevelSelected(int position);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
List<HashMap<String, String>> colorList = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < 6; i++){
HashMap<String, String> colorMap = new HashMap<String, String>();
colorMap.put("lvl", colorLevel[i]);
colorMap.put("img", Integer.toString(colorImages[i]));
colorList.add(colorMap);
}
String[] from = {"img", "lvl"};
int[] to = {R.id.colorLevelImg, R.id.colorLevelTxt};
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(),colorList, R.layout.fragment_b ,from, to);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
try{
mCallback = (OnColorSelectedListener) activity;
}catch(ClassCastException e){
throw new ClassCastException(activity.toString()
+ " must implement OnColorSelectedListener");
}
}
@Override
public void onListItemClick(ListView l, View v, int pos, long id){
Toast.makeText(getActivity(), "selected color :" + colorLevel[pos],
Toast.LENGTH_LONG).show();
mCallback.onColorLevelSelected(pos);
}
}
fragment_b.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
**android:id="@+id/curriculumParent"**
...
...
android:background="#FEFDFB">
<ImageView
android:id="@+id/colorLevelImg"
...
android:paddingBottom="10dp" />
<LinearLayout
...
android:orientation="vertical" >
<TextView
android:id="@+id/colorLevelTxt"
...
android:textSize="25sp" />
</LinearLayout>
</LinearLayout>
FragmentC.java
public class FragmentC extends Fragment{
public static final String TAG = "Fragment C";
public FragmentC(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
Log.i(TAG, "HI");
return inflater.inflate(R.layout.fragment_c, container, false);
}
}
fragment_c.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFCC00" >
<TextView
android:id="@+id/editText1"
...
android:layout_margin="16dp"
android:text="This is the C Fragment that will replace the B Fragment" >
</TextView>
</RelativeLayout>