A Fragment is a piece of an activity which enable more modular activity design. It will not be wrong if we say, a Fragment is a kind of sub-activity.
A Fragment represents a behavior or a portion of user interface in an activity. You can combine
multiple fragments in a single activity yo build a multi-pane UI and reuse a fragment in multiple
activities.You can think of a fragment as a modular section of an activity, which has its own
lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).
A Fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle. For example, when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments. However, while an activity is running (it is in the resumed lifecycle state), you can manipulate each fragment independently, such as add or remove them. When you perform such a fragment transaction, you can also add it to a back stack that's managed by the activity—each back stack entry in the activity is a record of the fragment transaction that occurred. The back stack allows the user to reverse a fragment transaction (navigate backwards), by pressing the Back button.
A Fragment represents a behavior or a portion of user interface in an activity. You can combine
multiple fragments in a single activity yo build a multi-pane UI and reuse a fragment in multiple
activities.You can think of a fragment as a modular section of an activity, which has its own
lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).
A Fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle. For example, when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments. However, while an activity is running (it is in the resumed lifecycle state), you can manipulate each fragment independently, such as add or remove them. When you perform such a fragment transaction, you can also add it to a back stack that's managed by the activity—each back stack entry in the activity is a record of the fragment transaction that occurred. The back stack allows the user to reverse a fragment transaction (navigate backwards), by pressing the Back button.
When you add a fragment as a part of your activity layout, it lives in a
Example:- ViewGroup
inside the activity's view hierarchy and the fragment defines its own view
layout.
You can insert a fragment into your activity layout by declaring the fragment in the activity's
layout file, as a <fragment>
element, or from your application code by adding it to an
existing ViewGroup
. However, a fragment is not required to be a part of the
activity layout; you may also use a fragment without its own UI as an invisible worker for the
activity.
Here I Created simple Example how we are using Fragments in an Activity an how make Transaction between Activity and a Fragment in Android
Create method inside the main activity name as Loadfragment
Call this method inside the Activity onCreate() Method for adding fragment first inside the
Activity when it will open Like:-
Create Three Fragments named as Fragmentfirst,FragmentSecond,FragmentThird
For Create Fragment follow the steps :-
1. click on File->New->Fragment->Fragment(Blank)
Source Code Download link:-https://drive.google.com/open?id=10z-FF7PsH5acjFSTBGL1vLqm99IoK0rl
Create method inside the main activity name as Loadfragment
public void Loadfragment(Fragment fragment){ // create a FragmentManager
FragmentManager fragmentManager= getFragmentManager(); // create a FragmentTransaction to begin the transaction and replace the Fragment
FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction(); if(fragmentTransaction==null){ fragmentTransaction.add(R.id.frameLayout,fragment); }else { // replace the FrameLayout with new Fragment
fragmentTransaction.replace(R.id.frameLayout, fragment); } fragmentTransaction.commit(); // save the changes}This method takes the name of fragment as a parameter which you can add inside the Activity.
Call this method inside the Activity onCreate() Method for adding fragment first inside the
Activity when it will open Like:-
public class MainActivity extends AppCompatActivity { private TextView mTextMessage; private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.navigation_home: Loadfragment(new Fragmentfirst()); return true; case R.id.navigation_dashboard: Loadfragment(new FragmentSecond()); return true; case R.id.navigation_notifications: Loadfragment(new FragmentThird()); return true; } return false; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); Loadfragment(new Fragmentfirst()); } public void Loadfragment(Fragment fragment){ // create a FragmentManager
FragmentManager fragmentManager= getFragmentManager(); // create a FragmentTransaction to begin the transaction and replace the Fragment
FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction(); if(fragmentTransaction==null){ fragmentTransaction.add(R.id.frameLayout,fragment); }else { // replace the FrameLayout with new Fragment
fragmentTransaction.replace(R.id.frameLayout, fragment); } fragmentTransaction.commit(); // save the changes} }
XML File
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"tools:context="com.ss.fragmentexanple.MainActivity"> <FrameLayout android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="55dp" /> <android.support.design.widget.BottomNavigationView android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>
Create Three Fragments named as Fragmentfirst,FragmentSecond,FragmentThird
For Create Fragment follow the steps :-
1. click on File->New->Fragment->Fragment(Blank)
Source Code Download link:-https://drive.google.com/open?id=10z-FF7PsH5acjFSTBGL1vLqm99IoK0rl
Comments
Post a Comment