0

when i create blank Activity with fragment in android studio what is actually done ! Specifically how they attached together ? in which line of code and how this done ?

NoChinDeluxe
  • 3,446
  • 1
  • 16
  • 29
Mohamed Yehia
  • 400
  • 1
  • 5
  • 15

3 Answers3

2

What is actually done?

It creates a template out of the following files.

  • One Activity with a layout
  • One ActivityFragment with a layout
  • One strings.xml value for the title of the Activity
  • One res/menu resource for the Toolbar menu
  • Adds an <activity> section to the AndroidManifest.xml

Screenshot

How are they attached together? In which line of code?

In the onCreate for the Activity, you set the layout

setContentView(R.layout.activity_main);

Which attaches the Fragment with a <fragment> tag like this, which is basically a FrameLayout.

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/fragment"
          android:name="com.androidstack.app.MainActivityFragment"
          tools:layout="@layout/fragment_main"
          android:layout_width="match_parent"
          android:layout_height="match_parent"/>
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

Please read about it here. Google's docs should help you understand a bit.

However a short version is this:

in the Activity's onCreate:

  • Activity's view is drawn.
  • Fragment is instantiated.

in the fragments's onAttach:

  • Fragment is attached to Activity

in the fragment's onCreate:

  • fragment is created

in the Fragment's onViewCreated:

  • Fragment's view is drawn.
Cory Roy
  • 5,379
  • 2
  • 28
  • 47
0

Just think of it as an activity within an activity. It is essentially the same things as changing the activity by using an intent but is just within it. So there is a parent activity, within it is the frame or layout that holds the fragment. When the parent activity is done loading it will begin to load the contents of the layout's, which is your fragment. Contents of that fragment can be accessed by using v.OnclickListener and so on

Ethan
  • 1,905
  • 2
  • 21
  • 50