locked
Converting Activity into Fragment RRS feed

  • Question

  • User130935 posted

    If i have nicely working Activity, and i want to put it on screen with another Activity, i should make a Fragment out of one of them, right? So how do I convert Activity (inherited from FragmentActivity) into a Fragment? Any ways?

    Thursday, July 23, 2015 3:53 PM

Answers

  • User48032 posted

    At a minimum you should be able to easily convert it as follows: 1) change the inheritance in the class from FragmentActivity to Fragment 2) remove the Activity attribute from the class (ie. remove this: [Activity(Theme = ...)]) 3) remove the OnCreate override 4) add the OnCreateView override and inflate your view here (SetContent is only valid for activity): public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { var view = inflater.Inflate(layoutResourceId, container, false); return view; }

    5) If you have any other Activity-specific code in here you will need to remove it or repurpose it for the fragment depending on what it is 6) add a FrameLayout in the main Activity in which you want to use this fragment 7) remove the StartActivity line of code that loads the old Activity and instead use the FragmentManager in the Main Activity to manage your fragment (ie. add, replace, or remove within the Activity). For example: FragmentManager.BeginTransaction() .Replace(fragmentContainerId, fragment) .Commit();

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, July 23, 2015 6:44 PM
  • User48032 posted

    In FragmentAct.cs, reference SupportFragmentManager through the Activity that is hosting it. Fragments expose an Activity property which provides a handle to its parent Activity, so simply call it like this: Activity.SupportFragmentManager

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, July 23, 2015 7:03 PM

All replies

  • User48032 posted

    At a minimum you should be able to easily convert it as follows: 1) change the inheritance in the class from FragmentActivity to Fragment 2) remove the Activity attribute from the class (ie. remove this: [Activity(Theme = ...)]) 3) remove the OnCreate override 4) add the OnCreateView override and inflate your view here (SetContent is only valid for activity): public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { var view = inflater.Inflate(layoutResourceId, container, false); return view; }

    5) If you have any other Activity-specific code in here you will need to remove it or repurpose it for the fragment depending on what it is 6) add a FrameLayout in the main Activity in which you want to use this fragment 7) remove the StartActivity line of code that loads the old Activity and instead use the FragmentManager in the Main Activity to manage your fragment (ie. add, replace, or remove within the Activity). For example: FragmentManager.BeginTransaction() .Replace(fragmentContainerId, fragment) .Commit();

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, July 23, 2015 6:44 PM
  • User130935 posted

    @LoriLalonde I've tried sth you described before, and there is a problem on a screenshot 1 I don't know what to do with. Also I attached the way I'm trying to invoke the Fragment I remade Activity in (2nd screenshot), but I don't know if it works because of problem at screenshot 1.

    Thursday, July 23, 2015 6:58 PM
  • User48032 posted

    In FragmentAct.cs, reference SupportFragmentManager through the Activity that is hosting it. Fragments expose an Activity property which provides a handle to its parent Activity, so simply call it like this: Activity.SupportFragmentManager

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, July 23, 2015 7:03 PM
  • User130935 posted

    @LoriLalonde Thank you su much, everything works great now, that is so simple and so genius :smiley:

    Thursday, July 23, 2015 11:44 PM
  • User48032 posted

    You're welcome!

    Friday, July 24, 2015 2:23 AM