Fragment
Fragment or Sub Activity: A part of an activity.
Why we use Fragment:
1. Space Utilize
2. Code Reuse. cause fragment can be used just as a template. [ ex: login form use in 5 activity but code once ]
we can change a part of any activity without changing full activity... that part of activity name is fragment.
A
the perfect example for a fragment is : [ Tab View ]
for Making Fragment:
1. Make Some Fragment XML & JAVA file:
2. Design Every Fragment Layout As your wish.
3. Now In an Activity ( where you want to host all fragments ) take a place/part/portion where you wanna attach those fragments. [ u can use any layout but here i use Framelayout. ]
4. Now Write Some Code in Java File [ activity.java ] to Attach or Add or Replace fragments in fragment container/part/portion in that activity.
5. Now If you want to change the Fragments onClick of any button or item then :
Why we use Fragment:
1. Space Utilize
2. Code Reuse. cause fragment can be used just as a template. [ ex: login form use in 5 activity but code once ]
we can change a part of any activity without changing full activity... that part of activity name 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. 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).the perfect example for a fragment is : [ Tab View ]
for Making Fragment:
1. Make Some Fragment XML & JAVA file:
2. Design Every Fragment Layout As your wish.
3. Now In an Activity ( where you want to host all fragments ) take a place/part/portion where you wanna attach those fragments. [ u can use any layout but here i use Framelayout. ]
4. Now Write Some Code in Java File [ activity.java ] to Attach or Add or Replace fragments in fragment container/part/portion in that activity.
FragmentManager fragmentManager = getSupportFragmentManager(); // fragment manager suport neya hoiche tai.
FragmentTransaction ft = fragmentManager.beginTransaction();
/*now add e default fragment in FragmentTransaction cause when main activity run for firsttime then
it/fragment container should not be empty*/
FragmentOne fragmentOne = new FragmentOne();
ft.add(R.id.FragmentContainer,fragmentOne);
//this code is for back button. it back to previous fragment
ft.addToBackStack(null);
// while changing/replacing fragment it make animation/effect like fade
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
5. Now If you want to change the Fragments onClick of any button or item then :
public void changeFragment(View view) {
Fragment fragment = null;
switch (view.getId())
{
case R.id.FragmentOne:
fragment = new FragmentOne();
break;
case R.id.FragmnetTwo:
fragment = new FragmentTwo();
break;
case R.id.FragmentThree:
fragment = new FragmentThree();
break;
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.FragmentContainer,fragment); // here we use replace cause it replace the previous fragment
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}
💟💟💟💟💟💟💟💟💟💟💟💟💟💟💟💟
Enough For Today

মন্তব্যসমূহ
একটি মন্তব্য পোস্ট করুন