0

I had a ListActivity where when I pressed one button, I started an intent showing another ListActivity. Now, I have to change these ListActivities to ListFragments. I do this to inflate the layout:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    /**Inflate the layout for this fragment*/
    return inflater.inflate(R.layout.recent_calls, container, false);
}

And to start the intent, now, as I a fragment, I do this:

CallDetailActivity Fragment_detail = new CallDetailActivity();
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.recent_calls, Fragment_detail);
        transaction.addToBackStack(null);
        transaction.commit();

My question is, where I put R.id.recent_calls in replace, I'm calling to the FrameLayout id of the same layout that I have initialized with the onCreateView. Is this ok? Or there would be another way to replace the actual layout with another when using fragments? something like the intent does for the activites.

UPDATE--

I'm having an error on the .replace as it is showing me "The method replace(int, fragment) in the type fragmenttransaction is not applicable for the arguments (int, CallDeateilActivity)"

UPDATE 2--

<FrameLayout 
android:id="@+id/recent_calls"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView 
    android:id="@android:id/list"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:scrollbarStyle="outsideOverlay"/>

<TextView 
    android:id="@android:id/empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Call log is empty"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge"/>

masmic
  • 3,526
  • 11
  • 52
  • 105
  • You have to keep one layout as root/parent and use that view's id to replace the fragment – SweetWisher ツ Nov 07 '13 at 11:12
  • your code looks fine. – Raghunandan Nov 07 '13 at 11:13
  • I've updated the post with the error – masmic Nov 07 '13 at 11:15
  • @masmic_87 post your xml looks like `recent_calls` is a xml . you need the id of a view group and replace existing fragment with `Fragment_detail`. Also what is `CallDetailActivity` a fragment or activity – Raghunandan Nov 07 '13 at 11:18
  • CallDetailActivity must extend Fragment – SweetWisher ツ Nov 07 '13 at 11:20
  • Yes, CallDetailActivity is called that way but extends fragment. I'll update now the post with the xml – masmic Nov 07 '13 at 11:23
  • @masmic_87 you need to use layout for `fragment` here instead of `recent_call.xml` return `inflater.inflate(R.layout.recent_calls, container, false);`. What is your min sdk in manifest – Raghunandan Nov 07 '13 at 11:27
  • Ok, so I have to call the id instead of the layout always in that case, true? thanks! – masmic Nov 07 '13 at 11:28
  • @masmic_87 no you need to have a layout for `CallDetailActivity` and use that instead – Raghunandan Nov 07 '13 at 11:30
  • yes, I have it, but I inflate this layout on the onCreateView method of the CallDetailActivity. The question is if in the .replace() method, to change one fragment to another, the first id that I enter is the "id of the current layout's father – masmic Nov 07 '13 at 11:52

1 Answers1

1

Seems like you've mixed Fragments from support library and Fragments from native android.

You have to choose what min. version of sdk you want to support and only then decide what Fragments to use.

My question is, where I put R.id.recent_calls in replace, I'm calling to the FrameLayout id of the same layout that I have initialized with the onCreateView. Is this ok?

I prefer to use more OOP - you can create one controller that handle translations etc from you fragment send callback to switch fragments.

Check this question too: Fragment add or replace not working

Community
  • 1
  • 1
dilix
  • 3,761
  • 3
  • 31
  • 55