Please visit my website for latest post about the difference between Add & Replace method and use of Back Stack.
http://dipenpatel.co.in/understanding-android-fragmentsadd-replace-method-and-back-stack/
Adding here Fragment demo which "Add" & "Replace" fragment in fragment holder.
Adding demo app for::
We will add different layout xml in different layout folder "layout" & "layout-land" to render differently on different orientation.
There are two Fragment & two Button, first button will "Replace" first fragment container and second button will "Add" fragment in second fragment container.
When we "Replace" fragment it will detach existing fragment and "Add" new fragment.
When we "Add" fragment it will add new fragment and existing fragment remain same.
We can maintain back stack of fragment which can be accessible later.
/**
* We can add name to this back stack and we can access this fragment by name later.
Otherwise we can pass null to parameter*/
fragmentTwoTransaction.addToBackStack(null);
Note:
I have added every fragment method name in StringBuilder when this method execute and Toast this StringBuilder onDetach of fragment. You can uncomment the Toast line in onDetach method and check fragment lifecycle.
About Code:::
LauncherActivity.java
/**
* @author dipenp
*
*/
public class LauncherActivity extends Activity {
private Button replaceFragmentButton, addFragmentButton;
private static int FRAGMENT_ONE_POSITION = 0, FRAGMENT_TWO_POSITION = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**************************************************
* Adding layout with two Button & two Fragment.
* One button to "Replace" fragment in first fragment & one button to "Add" fragment in second fragment.
* We have added same layout with different structure in different folder one in layout & one in layout-land(to display when device in landscape mode)
**************************************************/
setContentView(R.layout.activity_launcher);
replaceFragmentButton = (Button)findViewById(R.id.buttonOne);
addFragmentButton = (Button)findViewById(R.id.buttonTwo);
/**
* When we click on this button it will "Replace" existing fragment and add new one.
*/
replaceFragmentButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction fragmentOneTransaction = getFragmentManager().beginTransaction();
switch (FRAGMENT_ONE_POSITION) {
case 0:
FRAGMENT_ONE_POSITION++;
fragmentOneTransaction.replace(R.id.fragment_one_holder, new FragmentOne(LauncherActivity.this));
break;
case 1:
FRAGMENT_ONE_POSITION++;
fragmentOneTransaction.replace(R.id.fragment_one_holder, new FragmentTwo(LauncherActivity.this));
break;
case 2:
FRAGMENT_ONE_POSITION = 0;
fragmentOneTransaction.replace(R.id.fragment_one_holder, new FragmentThree(LauncherActivity.this));
break;
default:
break;
}
// fragmentOneTransaction.addToBackStack(null);
fragmentOneTransaction.commit();
}
});
/**
* When we click on this button it will "Add" new fragment.
*/
addFragmentButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction fragmentTwoTransaction = getFragmentManager().beginTransaction();
switch (FRAGMENT_TWO_POSITION) {
case 0:
FRAGMENT_TWO_POSITION++;
fragmentTwoTransaction.add(R.id.fragment_two_holder, new FragmentOne(LauncherActivity.this));
break;
case 1:
FRAGMENT_TWO_POSITION++;
fragmentTwoTransaction.add(R.id.fragment_two_holder, new FragmentTwo(LauncherActivity.this));
break;
case 2:
FRAGMENT_TWO_POSITION = 0;
fragmentTwoTransaction.add(R.id.fragment_two_holder, new FragmentThree(LauncherActivity.this));
break;
default:
break;
}
/**
* Adding current fragment to back stack which can be pop back when we need.
* We can add name to this back stack and we can access this fragment by name later.
*/
fragmentTwoTransaction.addToBackStack(null);
fragmentTwoTransaction.commit();
}
});
}
/**************************************
* Overriding onBackPressed button and pop backStack fragment.
*
**************************************/
@Override
public void onBackPressed() {
FragmentManager fm = getFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
fm.popBackStack();
} else {
super.onBackPressed();
}
}
}
You can download demo code from below url:
https://drive.google.com/file/d/0B0mH97AUwQqhMXVkc0N6VkY0cGM/edit?usp=sharing
http://dipenpatel.co.in/understanding-android-fragmentsadd-replace-method-and-back-stack/
Adding here Fragment demo which "Add" & "Replace" fragment in fragment holder.
Adding demo app for::
- Fragment
- Add & Replace fragment
- Different UI for different orientation
- Maintaining BackStack of Fragment
1. Portrait UI 2. Lanscape UI
We will add different layout xml in different layout folder "layout" & "layout-land" to render differently on different orientation.
There are two Fragment & two Button, first button will "Replace" first fragment container and second button will "Add" fragment in second fragment container.
When we "Replace" fragment it will detach existing fragment and "Add" new fragment.
When we "Add" fragment it will add new fragment and existing fragment remain same.
We can maintain back stack of fragment which can be accessible later.
/**
* We can add name to this back stack and we can access this fragment by name later.
Otherwise we can pass null to parameter*/
fragmentTwoTransaction.addToBackStack(null);
Note:
I have added every fragment method name in StringBuilder when this method execute and Toast this StringBuilder onDetach of fragment. You can uncomment the Toast line in onDetach method and check fragment lifecycle.
About Code:::
LauncherActivity.java
/**
* @author dipenp
*
*/
public class LauncherActivity extends Activity {
private Button replaceFragmentButton, addFragmentButton;
private static int FRAGMENT_ONE_POSITION = 0, FRAGMENT_TWO_POSITION = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**************************************************
* Adding layout with two Button & two Fragment.
* One button to "Replace" fragment in first fragment & one button to "Add" fragment in second fragment.
* We have added same layout with different structure in different folder one in layout & one in layout-land(to display when device in landscape mode)
**************************************************/
setContentView(R.layout.activity_launcher);
replaceFragmentButton = (Button)findViewById(R.id.buttonOne);
addFragmentButton = (Button)findViewById(R.id.buttonTwo);
/**
* When we click on this button it will "Replace" existing fragment and add new one.
*/
replaceFragmentButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction fragmentOneTransaction = getFragmentManager().beginTransaction();
switch (FRAGMENT_ONE_POSITION) {
case 0:
FRAGMENT_ONE_POSITION++;
fragmentOneTransaction.replace(R.id.fragment_one_holder, new FragmentOne(LauncherActivity.this));
break;
case 1:
FRAGMENT_ONE_POSITION++;
fragmentOneTransaction.replace(R.id.fragment_one_holder, new FragmentTwo(LauncherActivity.this));
break;
case 2:
FRAGMENT_ONE_POSITION = 0;
fragmentOneTransaction.replace(R.id.fragment_one_holder, new FragmentThree(LauncherActivity.this));
break;
default:
break;
}
// fragmentOneTransaction.addToBackStack(null);
fragmentOneTransaction.commit();
}
});
/**
* When we click on this button it will "Add" new fragment.
*/
addFragmentButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction fragmentTwoTransaction = getFragmentManager().beginTransaction();
switch (FRAGMENT_TWO_POSITION) {
case 0:
FRAGMENT_TWO_POSITION++;
fragmentTwoTransaction.add(R.id.fragment_two_holder, new FragmentOne(LauncherActivity.this));
break;
case 1:
FRAGMENT_TWO_POSITION++;
fragmentTwoTransaction.add(R.id.fragment_two_holder, new FragmentTwo(LauncherActivity.this));
break;
case 2:
FRAGMENT_TWO_POSITION = 0;
fragmentTwoTransaction.add(R.id.fragment_two_holder, new FragmentThree(LauncherActivity.this));
break;
default:
break;
}
/**
* Adding current fragment to back stack which can be pop back when we need.
* We can add name to this back stack and we can access this fragment by name later.
*/
fragmentTwoTransaction.addToBackStack(null);
fragmentTwoTransaction.commit();
}
});
}
/**************************************
* Overriding onBackPressed button and pop backStack fragment.
*
**************************************/
@Override
public void onBackPressed() {
FragmentManager fm = getFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
fm.popBackStack();
} else {
super.onBackPressed();
}
}
}
https://drive.google.com/file/d/0B0mH97AUwQqhMXVkc0N6VkY0cGM/edit?usp=sharing