Fragments - Do you have to use an Activity Wrapper around a fragment which comprises the whole Activity?

前端 未结 3 387
时光说笑
时光说笑 2021-02-04 01:54

Consider the sample app from developers.android.com

This describes using Fragments like so:

  • On a Phone you can use Fragment 1 on Activity A and fragment 2
3条回答
  •  闹比i
    闹比i (楼主)
    2021-02-04 02:35

    The online example doesn't fill in all the blanks. I'll try to answer your questions directly:

    "On the first example (the one with a phone) should you create an Activity with an xml file containing a single and an activity which only calls setContentView() on that xml and that's all?"

    You've started in the right place. But there's more to it than that. There's always more than one way to solve a problem in Android but a recommended way of generating the effect of having a dynamic number of fragments based on avail. real-estate is:

    1. Create layout XML files in /layout for the primary (default) targeted orientation/device/form-factor/SDK
    2. Create layout XML files for the smallest-width baseline for other targeted devices. You may also want to target other orientations, SDKs, etc.
    3. Each layout XML file will have it's own set of defined fragments
    4. In the Activity, check to see which fragments are present.

    Clearly an analogous strategy can be adopted for programmatic layouts.

    In your example in the original question (from Google's docs) you could have:

    • layout/main.xml :: this layout would only have Fragment 1
    • layout-sw600dp/main.xml :: this layout would have Fragments 1, 2

    Then in MainActivity.java you would check for the existence of each fragment. To do that you could use FragmentManager#findFragmentById() to have a check like: if findFragmentById() returns null for Fragment-2 then MainActivity knows the device has loaded layout/main.xml and only supports one fragment.

    Stepping 'back' from the example somewhat reveals that: prior to using Fragments you might have called Activity B from Activity A with startAcitityForResult(int). In the Fragment paradigm you probably only need to have a result from Fragment 2 cause something to happen in Fragment 1, so it's reasonable to have MainActivity be the gatekeeper for that. As you expand on the example you may see that in other apps, MainActivity may need to call other activities - for whatever reason. Perhaps you're targeting a large tablet with enough real estate for 3 fragments but on a handset that needs to be 3 activites. Things can get interesting but the Fragment API is fairly powerful.

    "Can you set a Fragment as an Activity or is a Wrapper always required when using fragments?"

    A Fragment is not an Activity. Indeed Fragments are loaded by Activities, so yes one might say a wrapper is always required. You're touching on aother subtle aspect of Fragments. Whereas Activities behave like MVC Controllers, Fragments could be called "mini-controllers" due to their lifecycle which both resembles and executes alongside an Activity. Again, the Fragment's lifecycle is contained inside ("wrapped by") the lifecycle of the Activity managing the Fragment. I recommend becoming familiar with the Fragment lifecycle documented at http://developer.android.com/guide/topics/fundamentals/fragments.html#Lifecycle.

提交回复
热议问题