Android Fragments
Android Fragments
Android Fragments
In Android, the fragment is the part of Activity which represents a portion of
User Interface(UI) on the screen.
The UI flexibility on all devices improves the user experience and adaptability
of the application.
Fragments can exist only inside an activity as its lifecycle is dependent on the
lifecycle of host activity.
For example, if the host activity is paused, then all the methods and
operations of the fragment related to that activity will stop functioning, thus
fragment is also termed as sub-activity. Fragments can be added, removed,
or replaced dynamically i.e., while activity is running.
1. Single Fragment: Display only one single view on the device screen. This
type of fragment is mostly used for mobile phones.
2. List Fragment: This Fragment is used to display a list-view from which the
user can select the desired sub-activity. The menu drawer of apps like
Gmail is the best example of this kind of fragment.
3. Fragment Transaction: This kind of fragments supports the transition
from one fragment to another at run time. Users can switch between
multiple fragments like switching tabs.
Each fragment has it’s own lifecycle but due to the connection with the Activity it
belongs to, the fragment lifecycle is influenced by the activity’s lifecycle.
3
4
5
Description
Methods
The very first method to be called when the fragment has been
associated with the activity. This method executes only once
during the lifetime of a fragment.
onAttach() When we attach fragment(child) to Main(parent) activity then it
call first and then not call this method any time(like you run an
app and close and reopen) simple means that this method call
only one time.
It indicates that the user is leaving the fragment. System call this
onPause()
method to commit the changes made to the fragment.
Description
Methods
activity_main.xml
File: activity_main.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="fill_parent"
6. android:layout_height="fill_parent"
7. tools:context="example.javatpoint.com.fragmentexample.MainActivity">
8.
9. <fragment
10. android:id="@+id/fragment1"
11. android:name="example.javatpoint.com.fragmentexample.Fragment1"
12. android:layout_width="0px"
13. android:layout_height="match_parent"
14. android:layout_weight="1"
15. />
16.
17. <fragment
18. android:id="@+id/fragment2"
19. android:name="example.javatpoint.com.fragmentexample.Fragment2"
20. android:layout_width="0px"
21. android:layout_height="match_parent"
22. android:layout_weight="1"
23. />
24.
25. </LinearLayout>
File: fragment_fragment1.xml
8
1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2. xmlns:tools="http://schemas.android.com/tools"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. android:background="#F5F5DC"
6. tools:context="example.javatpoint.com.fragmentexample.Fragment1">
7.
8. <!-- TODO: Update blank fragment layout -->
9. <TextView
10. android:layout_width="match_parent"
11. android:layout_height="match_parent"
12. android:text="@string/hello_blank_fragment" />
13.
14. </FrameLayout>
File: fragment_fragment2.xml
1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2. xmlns:tools="http://schemas.android.com/tools"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. android:background="#F0FFFF"
6. tools:context="example.javatpoint.com.fragmentexample.Fragment2">
7.
8. <!-- TODO: Update blank fragment layout -->
9. <TextView
10. android:layout_width="match_parent"
11. android:layout_height="match_parent"
12. android:text="@string/hello_blank_fragment" />
13.
14. </FrameLayout>
MainActivity class
File: MainActivity.java
1. package example.javatpoint.com.fragmentexample;
2.
3. import android.support.v7.app.AppCompatActivity;
4. import android.os.Bundle;
5.
9
File: Fragment1.java
1. package example.javatpoint.com.fragmentexample;
2. import android.os.Bundle;
3. import android.support.v4.app.Fragment;
4. import android.view.LayoutInflater;
5. import android.view.View;
6. import android.view.ViewGroup;
7.
8. public class Fragment1 extends Fragment {
9.
10. @Override
11. public void onCreate(Bundle savedInstanceState) {
12. super.onCreate(savedInstanceState);
13. }
14.
15. @Override
16. public View onCreateView(LayoutInflater inflater, ViewGroup container,
17. Bundle savedInstanceState) {
18. // Inflate the layout for this fragment
19. return inflater.inflate(R.layout.fragment_fragment1, container, false);
20. }
21. }
File: Fragment2.java
1. package example.javatpoint.com.fragmentexample;
2. import android.os.Bundle;
3. import android.support.v4.app.Fragment;
4. import android.view.LayoutInflater;
5. import android.view.View;
6. import android.view.ViewGroup;
10
Output:
11
Then Go to Fragment
13