Skip to main content

Fragment In Android

What is Fragment ?

*A Fragment represents a behavior or a portion of user interface in an Activity.

*You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a        fragment in multiple activities.

*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

*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.

*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. 

Example:

*How To call Activity To Fragment: 

public class MainActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //Now we Call Fragment 1 from Activity Dynamically 
        //we take one frame layout in mainactivity layout file for load dynamically more 
        and more layout
        Fragment1 fm1=new Fragment1();
        FragmentTransaction ft=getFragmentManager().beginTransaction();
        ft.replace(R.id.frmid,fm1);//here we take framelayout id and pass fragment object 
        ft.commit();

    }
}

*How To call  Fragment To Fragment: 

public class Fragment1 extends Fragment {
    @Nullable 
    @Override     
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle 
                          savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_fragment1, null);
        //Now we call Fragment2 From Fragment1        Fragment2 fm1=new Fragment2();
        FragmentTransaction ft=getFragmentManager().beginTransaction();
        ft.replace(R.id.frmid,fm1);//here we take framelayout id and pass fragment object 
        ft.commit();

        return v;
    }
}


 *How To call Fragment To Activity: 

public class Fragment1 extends Fragment {
        @Nullable 
        @Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                          Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_fragment1, null);
        //Now we call Fragment2 From Fragment1
        Intent i=new Intent(getActivity(),Second.class);
        startActivity(i);
        return v;
    }
}


Full Example With add to Backstack and Close all Fragment And Go to First Fragment Using

Dynamic Method We Clear All Fragment From Activity And We Also Direct Redirect From Last 

Fragment To First Fragment.

Enjoy.....

 

 

Download Full Source Code:Click Here








  


Comments

Popular posts from this blog

Create Shared Prefrence Via XML In Android

Download Source Code : Click Here Output:

Sandeep Maheshwari on zee news

Network Security Exception in Android Retrofit Cleartext HTTP traffic not permitted

Create One directory in res/xml Create file in xml directory Past give below code : < network-security-config > < base-config cleartextTrafficPermitted= "true" /> < domain-config > < domain includeSubdomains= "true" >www.xyz.com</ domain >> < trust-anchors > < certificates src= "system" /> </ trust-anchors > </ domain-config > </ network-security-config > Add one Line into AndroidMenifest.xm File in application tag : android :networkSecurityConfig= "@xml/yourfilename" android :usesCleartextTraffic= "true"