I am trying to use backpress on fragments. I am not able to fix it. Here is my code below.
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
if (position!=3){
pos = position;
}
switch (position) {
case 0:
fragment = new Profile();
break;
case 1:
fragment = new Products();
break;
case 2:
fragment = new Help();
break;
case 3:
DialogLogout(DrawerFragment.this, getString(R.string.logout), getString(R.string.cofirm_logout));
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment)
.commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(pos, true);
mDrawerList.setSelection(pos);
setTitle(navMenuTitles[pos]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
On Product Fragment I have list in which I again use to call another fragment.
listCards.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Fragment fragment = new Transactions();
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
// .replace(R.id.frame_container, fragment)
.remove(Products.this)
.add(R.id.frame_container, fragment) //replace(R.id.frame_container, fragment)
.addToBackStack(null)
.commit();
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
});
return rootView;
}
BackPress functionality on DrawerFragmentActivity is like below:
#Override
public void onBackPressed() {
FragmentManager fragmentManager = getSupportFragmentManager();
int count = fragmentManager.getBackStackEntryCount();
if (count > 0) {
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
} else {
super.onBackPressed();
}
}
Functionality would be like, DrawerFragmentActivity(Profile page by default)->Product->Transactions. Drawer Icon would be visible on Transactions screen as well, user can click my cards screen again while on transaction screen using drawer.
When user click on product it will again open transactions page, It's working fine. Now what happening is, when we click back on transaction it is coming on Product page, but When I again click on Product list screen(Frame) is overlapping with ProductsList and Transactions screen.
I am sorry if I it's confusing, Please ask if you don't understand. I can explain.
Thanks.
Fragment back press working code
public class ChiefFragment extends Fragment {
View view;
// public OnBackPressedListener onBackPressedListener;
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle args) {
view = inflater.inflate(R.layout.activity_chief, container, false);
getActivity().getActionBar().hide();
view.setFocusableInTouchMode(true);
view.requestFocus();
view.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.i(getTag(), "keyCode: " + keyCode);
if (keyCode == KeyEvent.KEYCODE_BACK) {
getActivity().getActionBar().show();
Log.i(getTag(), "onKey Back listener is working!!!");
getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
// String cameback="CameBack";
Intent i = new Intent(getActivity(), home.class);
// i.putExtra("Comingback", cameback);
startActivity(i);
return true;
} else {
return false;
}
}
});
return view;
}
}
Use below code for back pressed in fragment.
public class DashBoard extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dashboard, container, false);
rootView.setFocusableInTouchMode(true);
rootView.requestFocus();
rootView.setOnKeyListener(new View.OnKeyListener(){
#Override
public boolean onKey(View v, int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_BACK) {
getActivity().finish(); }
return true;
}
return false;
}
});
}
this code help you to implement your backpreesed in fragment.
I would recommend you to implement an interface to manage backstack. Here is a good blog post which would help you understand this process
Related
I am using Bottom Navigation Layout to show 5 fragments but the problem is after every onResume is called each fragment start lagging more and more.
This is my main activity with contain bottom navigation
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menu_home:
if (!selectedFragment.equalsIgnoreCase(Consts.MasterHomeFragment)) {
openMasterHomeFragment();
}
break;
case R.id.menu_wallet:
if (!isSessionActive()) {
showLoginBottomDialog();
return false;
} else {
if (!selectedFragment.equalsIgnoreCase(Consts.WalletFragment))
openWalletFragment();
}
break;
case R.id.menu_portfolio:
if (!isSessionActive()) {
showLoginBottomDialog();
return false;
} else {
if (!selectedFragment.equalsIgnoreCase(Consts.PortfolioFragment))
openPortfolioFragment();
}
break;
on each method i do
public void openPortfolioFragment() {
eventTracker.track(EventTracker.HOME_PORTFOLIO_NAV_CLICKED);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
PortfolioFragment portfolioFragment = new PortfolioFragment();
transaction.replace(R.id.activity_home_main_frame, portfolioFragment);
transaction.commit();
selectedFragment = Consts.PortfolioFragment;
}
public void openProfileFragment() {
eventTracker.track(EventTracker.HOME_ACCOUNT_NAV_CLICKED);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
ProfileFragment profileFragment = new ProfileFragment();
transaction.replace(R.id.activity_home_main_frame, profileFragment);
transaction.commit();
selectedFragment = Consts.ProfileFragment;
}
and oneach fragment onresume calling api
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_wallet, container, false);
unbinder = ButterKnife.bind(this, view);
initViewPager(view);
return view;
}
#Override
public void onResume() {
super.onResume();
loadDataFromApi();
}
and rendering data
#Override
public void onDataLoaded(MasterHomeResponse response) {
if (getActivity() != null && isAdded()) {
masterHomeResponse = response;
hideRefreshing();
initPortfolio(response);
initInbox(response.getInbox());
initAssets(response);
initTutorial(response);
initFeeds(response);
showBottomMoreFloatingButton();
boolean dismissIntro = sharedPreferenceHelper.getSamplePreference().getShowCase();
showGuideView(response, dismissIntro);
}
}
when ever i go to background and come back to application every fragment start lagging on scrolling
I was searching solution for this issue i have got this link Android Facebook SDK lowers app performance me also using facebook sdk for login purpose so making autologging value to false my issue resolved.
I want to refresh the content of the fragment when a user clicks the refresher icon which is in menu action bar.
My application has three fragments on one activity with view pager; I tried to refresh all of them by calling them in onOptionsItemSelected() and I performed transactions to them, the application crashes when a user clicks refresh menu.
I read this question, it is likely similar to mine, but I couldn't find an appropriate answer to settle my problem: android: menu item click event from fragment I read this article too: but nothing helped me: https://developer.android.com/guide/topics/ui/menus maybe I am not doing it in a right way.
My code of refreshing all three fragments in the activity are here below:
#Override
public boolean onOptionsItemSelected(MenuItem item){
Fragment sentMsg=getSupportFragmentManager().findFragmentByTag("fragmentSentMsg");
Fragment receivedMsg=getSupportFragmentManager().findFragmentByTag("fragmentReceivedMsg");
Fragment allMsg=getSupportFragmentManager().findFragmentByTag("fragmentAllMsg");
FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
switch (item.getItemId()){
case R.id.refresher_id:
fragmentTransaction.detach(sentMsg).attach(sentMsg).commit();
fragmentTransaction.detach(receivedMsg).attach(receivedMsg).commit();
fragmentTransaction.detach(allMsg).attach(allMsg).commit();
break;
}
return super.onOptionsItemSelected(item);
}
These are the code of a one fragment:
public class Page2_sent_msg extends Fragment {
//default constructor
public Page2_sent_msg(){}
#SuppressLint("ResourceType")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
final View Page2_sent_msg=inflater.inflate(R.layout.page2_sent_msg,container,false);
ListView sentMsgListView=(ListView)Page2_sent_msg.findViewById(R.id.sentMsgListview);
ArrayList<String> sentMsgArrayList=new ArrayList<String>();
SQLite_database_helper_class myDb=new SQLite_database_helper_class(getContext());
Cursor result=myDb.getting_sms_from_db();
if (result.moveToFirst()){
do {
if (!result.getString(3).equals("Sent message")){
continue;
}else{
sentMsgArrayList.add("SMS No : "+result.getString(0)+"\n"
+"Address : "+result.getString(1)+"\n"
+"Date : "+result.getString(2)+"\n"
+"Type : "+result.getString(3)+"\n"
+"Content : "+"\n________\n\n"+result.getString(4)+"\n");
}
}while (result.moveToNext());
}
ArrayAdapter<String>sentMsgAdapter=new ArrayAdapter<>(getContext(),android.R.layout.simple_list_item_1,sentMsgArrayList);
sentMsgListView.setAdapter(sentMsgAdapter);
sentMsgListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//this is what will happen when a user clicks one item from the lis view
}
});
Page2_sent_msg.setTag("sentMsg");
return Page2_sent_msg;
}
I do really need a help. Kind regards!
Just write the fragment transaction in onclick of refresh button
fragment = new HomeFragment();
onFrangmentChange(fragment, true, false);
onFrangmentChange function will be like this
private void onFrangmentChange(BaseFragment fragment, boolean replace,
boolean addBackstack) {
this.fragment = fragment;
fm = getFragmentManager();
ft = fm.beginTransaction();
if (replace) {
ft.replace(R.id.fragment, fragment);
} else {
ft.add(R.id.fragment, fragment);
}
if (addBackstack) {
ft.addToBackStack(fragment.getClass().getSimpleName());
}
ft.commit();
}
Note:BaseFragment is nothing but A fragment which extend Fragment.You can write the common functions like checking network connection,email validation etc.in this fragment.
Add a method refresh in your fragment like this
public void refreshFragment() {
sentMsgArrayList.clear();
sentMsgAdapter.notifyDataSetChanged();
SQLite_database_helper_class myDb=new SQLite_database_helper_class(getContext());
Cursor result=myDb.getting_sms_from_db();
if (result.moveToFirst()){
do {
if (!result.getString(3).equals("Sent message")){
continue;
}else{
sentMsgArrayList.add("SMS No : "+result.getString(0)+"\n"
+"Address : "+result.getString(1)+"\n"
+"Date : "+result.getString(2)+"\n"
+"Type : "+result.getString(3)+"\n"
+"Content : "+"\n________\n\n"+result.getString(4)+"\n");
}
}while (result.moveToNext());
sentMsgAdapter.notifyDataSetChanged
}
Then called it once the user taps on refresh
#Override
public boolean onOptionsItemSelected(MenuItem item){
Fragment sentMsg=getSupportFragmentManager().findFragmentByTag("fragmentSentMsg");
Fragment receivedMsg=getSupportFragmentManager().findFragmentByTag("fragmentReceivedMsg");
Fragment allMsg=getSupportFragmentManager().findFragmentByTag("fragmentAllMsg");
switch (item.getItemId()){
case R.id.refresher_id:
sentMsg.refreshFragment();
receivedMsg.refreshFragment();
allMsg.refreshFragment();
break;
}
return super.onOptionsItemSelected(item);
}
I want to go back to the previous fragment.I tried many method like addtobackstack(null) and all. But I did not get the solution. My problem is when I click on the back button it goes to the home fragment. But I did not want it. I want that when I click on the back button it go to the previous fragment. Can anyone tell me how can I do this ?
This is my onActivityCreated() method :-
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (i == KeyEvent.KEYCODE_BACK) {
getFragmentManager().popBackStack();
return true;
}
return false;
}
});
}
This is the first Fragment :-
Fragment fragment = new Packages();
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.container, fragment).addToBackStack(null).commit();
This is the second fragment :-
Fragment fragment = new DeliveryFrag();
mContext.getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.container, fragment).addToBackStack(null)
.commit();
This is the third Fragment :-
Fragment fragment = new paytm();
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.container, fragment).addToBackStack(null)
.commit();
I am doing the get the view and apply set onClickListner on it. but the program not enter in this method. I don't know why? Please can anyone tell me what I am doing wrong?
You have to save fragment in stack using fragmentManager.addToBackStack(<fragment tag>).commit();. Then try to do getFragmentManager().popBackStack();
you should call this method in onResume. like this:
#Override
public void onResume() {
super.onResume();
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
getFragmentManager().popBackStack();
return true;
}
return false;
}
});
}
Make all public Fragment like so
public Fragment currentFrag = null;
public Fragment f1 = new Packages();
public Fragment f2 = new DeliveryFrag();
public Fragment f3 = new paytm();
Then, on the button where changing Fragment, fill your null currentFrag with the next one
#Override
public void onClick(View v){
if(currentFrag == null || currentFrag == f1){
//open second fragment code
currentFrag = f2;
}else if(currentFrag == f2){
//open third fragment code
currentFrag = f3;
}
}
then you override the onBackPressed() method in your main activity
#Override
public void onBackPressed()
{
if (currentFrag == f1){
super.onBackPressed() // close the app
}else if(currentFrag == f2){
//calling your first fragment code
currentFrag = null;
}else if(currentFrag == f3){
//calling your second fragment code
currentFrag = f2;
}
}
I hope this helps because even I'm getting a bit confused
worked for me....you can try this..... may help you...
write addToBackStack(null) before commit();
and write following code in your onBackPressed() method.
FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 1) {
ApplicationLog.Log(TAG,"IN > 1");
fm.popBackStack(); //will redirect you previous visited fragment
}
else if(fm.getBackStackEntryCount()==1)
{
finish(); //will close application
}
I'm working on go-back function with my fragments. I use the action bar as return button but the function onOptionsItemSelected does not work (the function may be even not called)
This code is in my FragmentActivity:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
getActionBar().setHomeButtonEnabled(false);//disable the back button
this.getFragmentManager().popBackStack();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bottom_tab);
FragmentTabHost tabHost = (FragmentTabHost)findViewById(R.id.bottom_tab_host);
tabHost.setup(this, getSupportFragmentManager(), R.id.bottom_tab_content);
//tabHost.addTab(tabHost.newTabSpec("explore").setIndicator("explore"), ListTabWidget.class, null);
tabHost.addTab(tabHost.newTabSpec("browse").setIndicator("browse"), SearchList.class, null);
}
And this code is in my Fragment(A):
public class SearchList extends Fragment implements SearchView.OnQueryTextListener{
FragmentActivity searchActivity;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
searchActivity = (FragmentActivity) this.getActivity();
View searchView = inflater.inflate(R.layout.search_list, container, false);
SearchView searchBar = (SearchView)searchView.findViewById(R.id.browse_search);
searchBar.setIconifiedByDefault(false); //Showing text field in search
searchBar.setSubmitButtonEnabled(true);
searchBar.setOnQueryTextListener(this);
return searchView;
}
#Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
/*Intent intent = new Intent(Intent.ACTION_SEARCH, null, searchActivity, BrowseGrid.class);
intent.putExtra(SearchManager.QUERY, query);
startActivity(intent);*/
Bundle args = new Bundle();
args.putString("search_query", query);
BrowseGrid browseFragment = new BrowseGrid();
browseFragment.setArguments(args);
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.bottom_tab_content, browseFragment);
transaction.addToBackStack(null);
transaction.commit();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}
}
and Fragment(B):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.grid_list, container, false);
gridView = (GridView)view.findViewById(R.id.gridView);
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
return view;
}
The aim is very simple. Pressing the back button, Fragment goes back from B to A.
Am I missing something?
As I found that the stack actually has nothing to pop(I wanna know why...), I choose an alternative to do so.
I don't know whether it is the best solution but it seems work.
replace
getFragmentManager().popBackStack();
to
FragmentManager fm = getFragmentManager();
if (fm.getBackStackEntryCount()>0){
//Log.d("Bottom Tab", "popping backstack");
fm.popBackStack();
} else {
//Log.d("Bottom Tab", "nothing can pop");
super.onBackPressed();
}
Even the stack has nothing, it will override the onBackPressed() to go back.
How about try using the SupportFragmentManager on both Activity and Fragment(A)
currently the Fragment (A) is using
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
While in you activity onOptionsItemSelected is using
this.getFragmentManager().popBackStack();
change it to:
getSupportFragmentManager().popBackStack();
Change this line , as you are handling the FragmentTransaction from Fragment class not Activity itself. You need to get getParentFragment() first.
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
To
FragmentTransaction transaction = getActivity().getParentFragment().getFragmentManager().beginTransaction();
transaction.replace(R.id.bottom_tab_content, browseFragment);
I am implementing navigation drawer and works well. So i am calling fragment on navigation drawer click and it is also working and further more i am calling another fragment from Home page fragment and maintain the back stack for every fragment but the problem is back press from the child fragment i can't go to Home page fragment and just exited from application. I don't want this. What i want Click on
Navigation Drawer->HomePageFragment->AnotherChild Fragment(On List Item click of HomePageFragment)
but on back pressed without going to Homepage fragment its directly exit with application. Here is my code: (In Fragment Activity with Navigation Drawer)
class SlideitemListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
updateDisplay(position);
}
}
private void updateDisplay(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new ScheduleFragment();
break;
case 1:
fragment = new Result_Fragment();
break;
case 2:
fragment = new Live_Match_Fragment();
break;
case 3:
// fragment = new Live_Match_Fragment();
break;
case 4:
fragment = new Team_Fragment();
break;
default:
break;
}
if (fragment != null) {
fragmentManager = getFragmentManager();
fragmentManager.popBackStackImmediate("0", 0);
int count = fragmentManager.getBackStackEntryCount();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.frame_container, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(String.valueOf(count)).commit();
Log.e("Count in Activiy", ""+count);
// update selected item and title, then close the drawer
setTitle(menutitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
Now in BackPressed() in FragmentActivity.
#Override
public void onBackPressed() {
if (fragmentManager.getBackStackEntryCount() <= 1) {
finish();
return;
}
super.onBackPressed();
}
Now calling another child fragment from HomePage fragment on Listview item click.
team_lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
TeamDetailFragment myDetailFragment = new TeamDetailFragment();
FragmentManager fragmentManager = getFragmentManager();
int count = fragmentManager.getBackStackEntryCount();
Log.e("Count in Fragment", "" + count);
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction
.replace(R.id.frame_container, myDetailFragment)
.setTransition(
FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(String.valueOf(count)).commit();
}
});
So anybody knows then help me. Help will be appreciate.
There is an issue with nested fragments in android
https://code.google.com/p/android/issues/detail?id=40323
Android doesn't handle back press well if transactions were in nested fragments.
To surpass this i am using the following fix inside My Activity
#Override
public void onBackPressed() {
// If the fragment exists and has some back-stack entry
if (myFragment != null && myFragment.getChildFragmentManager().getBackStackEntryCount() > 0) {
// Get the fragment fragment manager - and pop the backstack
myFragment.getChildFragmentManager().popBackStack();
}
// Else, nothing in the direct fragment back stack
else {
// Let super handle the back press
super.onBackPressed();
}
}
I've solved in the following way:
1st solution
#Override
public void onBackPressed() {
FragmentManager fragmentManager = getFragmentManager();
int backCount = fragmentManager.getBackStackEntryCount();
if(backCount > 1) {
super.onBackPressed();
} else {
finish();
}
}
2nd solution
#Override
public void onBackStackChanged() {
FragmentManager fragmentManager = getFragmentManager();
int backCount = fragmentManager.getBackStackEntryCount();
if(backCount == 0) {
finish();
}
}
These allow you to exploit the Android stack when you replace your fragment:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, yourFragment)
.addToBackStack(null).commit();
When you press back button inside your fragment onBackPressed() method of your activity will be called if you have declared that..So handling back button for fragments within navigation drawer can be one in this way..
MainActvity
public static boolean isMainActivityShown ;
public static boolean isFragment1Shown=false ;
public static boolean isFragment2Shown=false ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isMainActivityShown=true //inside onCreate method put isMainActivityShown true
.
.
.
{
Fragment currentFragment = new Fragment1();
isMainActivityShown=false; //when moving to fragment1
isFragment1Shown=true;
frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, currentFragment)
.commit();
}
.
.
}
#Override
public void onBackPressed() {
if(isMainActivityShown)
{
finish();
}
else if(isFragment1Shown)
{
//write the code to handle back button when you are in Fragment1
}
else if(isFragment2Shown)
{ //When you are in Fragment 2 pressing back button will move to fragment1
Fragment currentFragment = new Fragment1();
isFragment2Shown=false;
isFragment1Shown=true;
FragmentManager frgManager;
frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, currentFragment)
.commit();
}
}
Fragment1
Fragment currentFragment = new Fragment2();
MainActivity.isFragment1Shown=false;
MainActivity.isFragment2Shown=true;
frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, currentFragment)
.commit();
Now you can replace finish() for fragmentManager.popBackStack(), method of android.support.v4.app.FragmentManager