Open In App

Introduction to Fragments | Android

Fragment is a piece of an activity that enables a more modular activity design. A fragment encapsulates functionality so that it is easier to reuse within activities and layouts. Android devices exist in a variety of screen sizes and densities. Fragments simplify the reuse of components in different layouts and their logic. You can build single-pane layouts for handsets (phones) and multi-pane layouts for tablets. You can also use fragments also to support different layouts for landscape and portrait orientation on a smartphone. The below image shows how two UI modules defined by fragments can be combined into one activity for a tablet design but separated for a handset design.

 

Fragment Life Cycle

Android fragments have their own life cycle very similar to an android activity. 

Types of Fragments

Handling the Fragment Lifecycle

A Fragment exist in three states:

The effect of the activity lifecycle on the fragment lifecycle :

Defining and using fragments To define a new fragment we either extend the android.app.Fragment class or one of its subclasses. 

package com.saket.geeksforgeeks.demo;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DetailFragment extends Fragment {     
  
  @Override 
  public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){      
    View view = inflater.inflate(R.layout.fragment_rssitem_detail,container, false);                            
    return view;        
  }   
  public void setText(String text){    
    TextView view = (TextView) getView().findViewById(R.id.detailsText);        
    view.setText(text);    
  }
}
//code conntribute by rony 
package com.example.databinding.fragment
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.databinding.R
class fragment2 : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        var view =inflater.inflate(R.layout.fragment_fragment2, container, false)
        return view
    }
}


Fragment Transaction: While for an dynamic activity we are set buttons for an interactive UI. If we are set after clicking the button the fragment should appear then we have to get help from Fragment Manager. It handle all the fragment in an activity. We need to set fragment transaction with the help of fragment manager and and begin transaction, and then simply replace the layout of the fragment with desired place.

   /* this code is contributed by rohansadhukhan9 */
   Button B = findViewById(R.id.button);
   B.setOnClickListener(new View.OnClickListener) {
       @Override
       public void onClick(View v) {
           fragment f = new fragment();
           FragmentTransaction t = getSupportFragmentManeger().beginTransaction();
           t.replace(R.id.fragment_layout, f).commit();
       }
   }
//code contribute by rony
val B = findViewById<Button>(R.id.button)
B.setOnClickListener {
          replace(fragment2())
      }
    }
    private fun replace(fragment: Fragment) {
        val fragmentManager=supportFragmentManager
        val fragmentTransaction=fragmentManager.beginTransaction()
        fragmentTransaction.replace(R.id.fragment_layout,fragment)
        fragmentTransaction.commit()
    }
}

Article Tags :