1

What is true about a Fragment?

  • I can use a Fragment with out an Activity.
  • A fragment can have multiple activities in it.
  • An activity can contain multiple fragments in it.
  • A fragment designed in one activity can't be reused in other activity.

Answer: C An activity can contain multiple fragments in it.
Description:

2

When activity is moved to stopped state, then what will happen to the life cycle of a fragment which is in that activity?

  • fragments life cycle is not dependent on activity's. So there won't be any effect for fragment life cycle.
  • Option 1 is true because fragments are independent modules designed for better space usage
  • fragments will be moved to onStop
  • both fragments will be destroyed and recreated when activity reaches onResume again.

Answer: C fragments will be moved to onStop
Description: Since fragments are part of an Activity always, when Activity is moved to stopped state, then automatically fragments in it will be moved to stopped state.

Note: Fragments life cycle will be affected by activity's life cycle.

3

What is a Fragment, in android?

  • i. Fragments are designed to use the device UI space efficiently.
  • ii. Fragments are designed as a reusable components for multiple activities.
  • iii. Fragment will have its own life cycle, which will not dependent on Activity's life cycle.
  • iv. Fragment will have its own life cycle, which will be affected by Activity's life cycle as well.
  • i
  • i & ii
  • i, ii, & iii
  • i, ii, & iv

Answer: D i, ii, & iv

Description: Fragments is a new concept introduced in 3.0 version.

The basic purpose of fragments is:

1. Fragments are designed to use the device UI space efficiently.
When you are writing an application in android, then some people can download it into phone, some into tablets. If you see the space in tablets it will be little bigger than phones. You should be able to use that space efficiently. But you can't keep writing different applications one targeting for phone, and other targeting for tablets. In order to do it efficiently, i.e writing only application that can fit well with all screen sizes, we use fragments concept.
2. fragments are designed as a reusable UI components between more than one activity.
Once you design a fragment, you can view it as a detachable independent unit, so that you can plug it into any activity where ever there is a space. That means you can reuse the code designed for a fragment.
Fragment you can think of it like a sub activity, which sits with in an activity and which contributes its own UI to the activity screen.
Fragments are always part of an activity. With out an activity, a fragment will not exist. So your fragment life cycle will always be affected by activity life cycle.
An activity can contain more than one fragment. Similarly a fragment can be re used in multiple activities.

4

How to find a fragment in an activity?

  • findViewById(R.id.frag_id);
  • FragmentManager.findFragmentById(R.id.frag_id);
  • LayoutManager.findFragmentManager(R.id.frag_id);
  • Context.findFragmentById(R.id.frag_id);

Answer: B FragmentManager.findFragmentById(R.id.frag_id);
Description: To find if a fragment is part of an activity or not, we have to take the help of FragmentManager and use findFragmentById() function.

5

How to create different layout files portrait and landscape orientations?

  • it is not possible.
  • put two different files in layout folder, use one for portrait and other for landscape, based on orientation change.
  • use layout-port folder for portrait mode, and layout-land for landscape mode.
  • use layout-port folder for portrait mode, and layout folder for landscape mode.

Answer: D use layout-port folder for portrait mode, and layout folder for landscape mode.
Description: Create two different xml layout files with same name, put one under res/layout-port folder and other under res/layout folder.
Android will automatically pick layout-port folder xml file for portrait mode, and other for landscape mode.

6

How to return layout of a fragment? Which function to implement in Fragment class for it?

  • onCreate()
  • onCreateView()
  • getView()
  • getFragmentView()

Answer: B onCreateView()
Description: We have to implement onCreateView() to return the layout of fragment as part of fragments UI contribution.

7

Is it possible to have fragments without UI, in android?

  • Fragment will never have any UI.
  • Fragment generally will contribute its UI by using its own layout. But it might not have UI in some cases, as it is not mandatory.
  • Both option 1 & 2 are right.
  • none of the above are true. Because fragments should always have its own layout and contribute its UI.

Answer: B Fragment generally will contribute its UI by using its own layout. But it might not have UI in some cases, as it is not mandatory.
Description: Fragment generally will contribute its UI by using its own layout. But it might not have one in some cases.

8

How to create an activity which has fragment with listview?

  • use Activity, fragment, a layout which will have list view.
  • use activity & a ListFragment
  • Use only Activity with a layout which is having a list view in it.
  • Use only a ListFragment with out any activity.

Answer: B use activity & a ListFragment
Description: If a fragment is required to display a list view in it, then there is special purpose fragment called as ListFragment to achieve it.
So use activity with ListFragment.

9

Which of the below is not a Fragment class.

  • Fragment
  • DialogFragment
  • PreferenceFragment
  • ActivityFragment

Answer: D ActivityFragment
Description: There is nothing called as ActivityFragment class. Remaining are derived classes of Fragment class.

10

Which of the below functions are not part of Fragment life cycle?

  • onCreateView()
  • onDetach()
  • onActivityDestroyed()
  • onActivityCreated()

Answer: C onActivityDestroyed()
Description: onActivityDestroyed() is not the part of Fragment life cycle.

11

Which of the below are correct sequence of Fragment life cycle?

  • onAttach()
    onCreate()
    onCreateView()
    onStart()
    onActivityCreated()
    onResume()
  • onAttach()
    onCreateView()
    onCreate()
    onActivityCreated()
    onStart()
    onResume()
  • onAttach()
    onCreate()
    onCreateView()
    onStart()
    onActivityCreated()
    onResume()
  • onAttach()
    onCreate()
    onCreateView()
    onActivityCreated()
    onStart()
    onResume()

Answer: D onAttach()
onCreate()
onCreateView()
onActivityCreated()
onStart()
onResume()
Description:

12

Suppose X is a fragment activity, Y and Z are fragments where Y is the default screen displayed.If we have to open Z,then

  • Y must use fragment manager to open screen Z.
  • Y must use intent to open the screen Z.
  • Y must use fragment- transaction with fragment-manager and replace itself with screen Z.
  • None are correct.

Answer: C Y must use fragment- transaction with fragment-manager and replace itself with screen Z.
Description:

13

What is a fragment in android?

  • Fragment is a part of an activity, which contributes its own UI to that activity. Fragment can be thought like a sub activity.
  • Fragments are used to efficiently use the space in wider screen devices.
  • An activity may contain 0 or multiple number of fragments based on the screen size. A fragment can be reused in multiple activities, so it acts like a reusable component in activities.
  • all are true

Answer: D all are true
Description:

14

What is the difference between activity and fragment in android?

  • fragment is a part of an activity, which contributes its own UI to that activity. Fragment can be thought like a sub activity. Where as the complete screen with which user interacts is called as activity. An activity can contain multiple fragments.Fragments are mostly a sub part of an activity.
  • An activity may contain 0 or multiple number of fragments based on the screen size. A fragment can be reused in multiple activities, so it acts like a reusable component in activities.
  • A fragment can't exist independently. It should be always part of an activity. Where as activity can exist with out any fragment in it.
  • all are true

Answer: D all are true
Description:

15

Why fragments in android? What is the use?

  • Fragments are used to efficiently use the space on wider screen devices.
  • fragments are used as a means of solving compatibility issues when targeting an android application for various screen sizes, esp for both tablets and mobiles.
  • Fragments can be used to reuse the same piece of UI template in multiple activities, without rewriting the entire code. These are kind of reusable components which fits into an activity.
  • all are true

Answer: D all are true
Description: