So here is my navigationDrawer in image below has menu items ,and every single item add a fragment and load it own data , so how to prevent re-added a fragment once it's loaded, it's re-added the fragment and i don't want that.
here is what i tried :
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
if (menuItem.getItemId() == R.id.nav_home) {
makeTransaction(new Home(), "home_fragment");
toolbarTitle.setText("Home");
} else if (menuItem.getItemId() == R.id.nav_prizes) {
makeTransaction(new Prizes(), "prizes_fragment");
toolbarTitle.setText("Prizes");
} else if (menuItem.getItemId() == R.id.nav_myAccount) {
mDrawerLayout.closeDrawer(GravityCompat.START);
startActivity(new Intent(MainActivity.this, MyAccount.class));
} else if (menuItem.getItemId() == R.id.nav_contact_us) {
makeTransaction(new ContactUs(), "contactUs_fragment");
toolbarTitle.setText("Contact Us");
} else if (menuItem.getItemId() == R.id.nav_about_us) {
makeTransaction(new AboutUs(), "aboutUs_fragment");
toolbarTitle.setText("About Us");
} else if (menuItem.getItemId() == R.id.nav_share) {
mDrawerLayout.closeDrawer(GravityCompat.START);
}
return false;
}
public void makeTransaction(Fragment fragment, String tag) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fragment test = getFragmentManager().findFragmentByTag(tag);
getFragmentManager().executePendingTransactions();
if (test != null && test.isVisible()) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else if (test != null && test.isAdded()) {
getFragmentManager().popBackStack(tag,0);
} else {
mDrawerLayout.closeDrawer(GravityCompat.START);
transaction.setCustomAnimations(R.anim.enter_from_left,
R.anim.exit_to_right, 0, 0);
transaction.add(R.id.fragment_container, fragment, tag);
transaction.addToBackStack(tag);
transaction.commit();
}
}
I face similar problem that on clicking same item again and again it reload same fragment every time
For this you can make method
public boolean checkFragment(int position, Fragment mFragment) {
if ((mFragment instanceof "your_fragment_name"))
return true;
return false;
}
and before committing fragment
you can add this
if (checkFragment(position, mFragment)) return;
As
if (checkFragment(position, mFragment)) return;
transaction.setCustomAnimations(R.anim.enter_from_left,
R.anim.exit_to_right, 0, 0);
transaction.replace(R.id.fragment_container, fragment, tag);
transaction.addToBackStack(tag);
transaction.commit();
EDIT
Please replace your makeTransaction method with below code.
public void makeTransaction(Fragment fragment, String tag) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fragment test = getFragmentManager().findFragmentByTag(tag);
if (test != null && test.isVisible()) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else if (test != null) {
getFragmentManager().popBackStack(tag, FragmentManager.POP_BACK_STACK_INCLUSIVE);
// getSupportFragmentManager().popBackStackImmediate(tag, 0);
} else {
mDrawerLayout.closeDrawer(GravityCompat.START);
if (checkFragment(position, mFragment)) return;
transaction.setCustomAnimations(R.anim.enter_from_left,
R.anim.exit_to_right, 0, 0);
transaction.replace(R.id.fragment_container, fragment, tag);
transaction.addToBackStack(tag);
transaction.commit();
}
}
If source is an object variable, then instanceof is a way of checking to see if it is a Fragment or not.
So we use this property to check that if currently clicked item is instance of its fragment or not.
Declare variable for FragmentManager outside of the methods:
FragmentManager fm;
Inside somewhere in onCreate(), you initialize it.
fm = getFragmentManager();
In your makeTransaction() use fm instead of "getFragmentManager()"
FragmentTransaction transaction = fm.beginTransaction();
and etc..
This make your management easier, and you can find fragments using TAG like that:
Fragment fragment = ({FRAGMENT_CLASS}) fm.findFragmentByTag({TAG});
if (fragment == null) {
//add to transaction
} else {
//use existing thorugh fragment
}
If after that the fragment isn't null it'll have the link to the fragment which is created using that TAG.
if you create following fragment:
makeTransaction(new ContactUs(), "contactUs_fragment");
You would be able to get reference to it like this:
Fragment fragment = (ContactUs) fm.findFragmentByTag("contactUs_fragment");
and do with it anything you want.
Related
When i change tab of Bottom Bar for example when the home tab is selected and when i change the tab and select category tab in first time load data and it is not problem but when select again home tab again reload fragment and load data again.
how to resolve this problem and save state fragment.
My code:
bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
#Override
public void onTabSelected(int tabId) {
displayFragment(tabId);
}
});
private void displayFragment(int id) {
Fragment selectedFragment = null;
switch (id) {
case R.id.tab_home:
selectedFragment = HomeFragment.getInstance();
break;
case R.id.tab_category:
selectedFragment = CategoryFragment.getInstace();
break;
}
if (selectedFragment != null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.contentContainer, selectedFragment);
transaction.commit();
}
}
There are multiple ways you can handle preserving and updating your fragments data throughout its life cycle. I would look into the lifecycle of activity fragments to better grasp what applies to your context. From 30k feet, after you call .replace() the fragment's override onCreateView() should fire, here you can handle your update logic.
As I mentioned, context is big here. If you end up changing your transaction logic, it could impact the life cycle of your fragments. For example, if you were hiding/showing your fragments, using the override onHidden() or onResume() could be a better solution.
In addition, you also should consider how your navigation logic impacts and interacts with your activity's/fragment's backstack. In your current logic, you're creating a new instance of the fragment each time a tab is selected.
This is answer my question :
public class MainActivity extends AppCompatActivity {
FeedFragment feedFragment;
PublishFragment publishFragment;
ServicesFragment servicesFragment;
SearchFragment searchFragment;
ProfileFragment profileFragment;
FragmentTransaction ft;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomBar bottomBar = (BottomBar) findViewById(R.id.bottomBar);
feedFragment = new FeedFragment();
publishFragment = new PublishFragment();
servicesFragment = new ServicesFragment();
searchFragment = new SearchFragment();
profileFragment = new ProfileFragment();
bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
#Override
public void onTabSelected(#IdRes int tabId) {
if (tabId == R.id.feed_icon) {
GeneralUtils.logPrint("feed");
ft = getSupportFragmentManager().beginTransaction();
if (feedFragment.isAdded()) {
ft.show(feedFragment);
} else {
ft.add(R.id.fragment_container, feedFragment);
}
hideFragment(ft,publishFragment);
hideFragment(ft, servicesFragment);
hideFragment(ft,searchFragment);
hideFragment(ft,profileFragment);
ft.commit();
}
if (tabId == R.id.publish_icon) {
GeneralUtils.logPrint("publish");
ft = getSupportFragmentManager().beginTransaction();
if (publishFragment.isAdded()) {
ft.show(publishFragment);
} else {
ft.add(R.id.fragment_container, publishFragment);
}
hideFragment(ft,feedFragment);
hideFragment(ft, servicesFragment);
hideFragment(ft,searchFragment);
hideFragment(ft,profileFragment);
ft.commit();
}
if (tabId == R.id.services_icon) {
GeneralUtils.logPrint("services");
ft = getSupportFragmentManager().beginTransaction();
if (servicesFragment.isAdded()) {
ft.show(servicesFragment);
} else {
ft.add(R.id.fragment_container, servicesFragment);
}
hideFragment(ft,feedFragment);
hideFragment(ft, publishFragment);
hideFragment(ft,searchFragment);
hideFragment(ft,profileFragment);
ft.commit();
}
if (tabId == R.id.search_icon) {
GeneralUtils.logPrint("search");
ft = getSupportFragmentManager().beginTransaction();
if (searchFragment.isAdded()) {
ft.show(searchFragment);
} else {
ft.add(R.id.fragment_container, searchFragment);
}
hideFragment(ft,feedFragment);
hideFragment(ft,publishFragment);
hideFragment(ft,servicesFragment);
hideFragment(ft,profileFragment);
ft.commit();
}
if (tabId == R.id.profile_icon) {
GeneralUtils.logPrint("profile");
ft = getSupportFragmentManager().beginTransaction();
if (profileFragment.isAdded()) {
ft.show(profileFragment);
} else {
ft.add(R.id.fragment_container, profileFragment);
}
hideFragment(ft,feedFragment);
hideFragment(ft, publishFragment);
hideFragment(ft,servicesFragment);
hideFragment(ft,searchFragment);
ft.commit();
}
}
});
}
private void hideFragment(FragmentTransaction ft, Fragment f) {
if (f.isAdded()) {
ft.hide(f);
}
}
}
I am working on single activity based principle. But I am facing a problem when the same fragment is open again because its again adds in fragment backstack entry. So backstack contains multiple backstack entries for same fragment. This creates problem on back navigation.
Example :- A|B|C|D|A|C|A
So when I press back key same fragment is displaying multiple times. Is there any way to reuse the existing fragment from backstack entry.
I am managing my backstack like this :-
fragmentManager.beginTransaction().setCustomAnimations(R.anim.fragment_enter,
R.anim.fragment_exit, R.anim.pop_enter, R.anim.pop_exit).
add(R.id.frameLayout, fragment).addToBackStack(backStateName).commit();
Any kind of help will be appreciated.
private void createOrResumeFragment(String fragmentTag){
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
boolean fragmentPopped = manager.popBackStackImmediate (fragmentTag, 0);
Fragment fragment = manager.findFragmentByTag(fragmentTag);
if(!fragmentPopped && fragment == null){
//Create an new instance if it is null and add it to stack
fragment = new MyFragment();
ft.addToBackStack(fragmentTag);
}
ft.replace(R.id.framelayout, fragment);
ft.commit();
}
Trying this using the fragment list
See the Answer Here
Initialize the fragments list
static List<String> fragments = new ArrayList<String>();
on Start of first fragment on Activity add this
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.frame, new AFragment()).commit();
fragments.add("package.fragment.AFragment");
}
Code to fragment change and take in back stack
String backStateName = fragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
//fragment not in back stack, create it.
FragmentTransaction ft = manager.beginTransaction();
if(!fragments.contains(backStateName)) {
// ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
// ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
ft.replace(R.id.frame, fragment);
ft.addToBackStack(backStateName);
ft.commit();
fragments.add(backStateName);
System.out.println("backStateName" + fragments);
}
else
{
ft.replace(R.id.frame, fragment);
ft.commit();
}
onBackpressed
#Override
public void onBackPressed() {
if(fragments.size()>0)
{
fragments.remove(fragments.size()-1);
}
super.onBackPressed();
}
for back remove stack
final android.app.FragmentManager fm = getFragmentManager();
fm.addOnBackStackChangedListener(new android.app.FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
if (getSupportFragmentManager().getBackStackEntryCount() ==0) {
// dLayout.closeDrawers();
finish();
}
else
{
// dLayout.closeDrawers();
}
}
});
Before adding or replacing the fragment on backstack, check that if the fragment already in backstack or not.
boolean fragmentPopped = fragmentManager.popBackStackImmediate(backStateName, 0);
if (fragmentPopped) {
// fragment is popped from backStack
} else {
// add or replace your fragment here
}
public void changeFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment currentVisibleFragment = fragmentManager.findFragmentById(R.id.container);
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, fragment, fragment.getClass().getSimpleName());
if (!currentVisibleFragment.getClass().getSimpleName().trim().equalsIgnoreCase(fragment.getClass().getSimpleName().trim())) {
for (int i = fragmentManager.getBackStackEntryCount() - 1; i > 0; i--) {
if (fragmentManager.getBackStackEntryAt(i).getName().equalsIgnoreCase(fragment.getClass().getSimpleName())) {
fragmentManager.popBackStack(fragmentManager.getBackStackEntryAt(i).getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
}
fragmentTransaction.addToBackStack(fragment.getClass().getSimpleName());
} else {
fragmentManager.popBackStack();
fragmentTransaction.addToBackStack(fragment.getClass().getSimpleName());
}
fragmentTransaction.commit();
}
boolean doubleBackToExitPressedOnce = false;
#Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
super.onBackPressed();
} else {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
finish();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Are you sure you want to exit?", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
}
}
Call the method to replace fragment with single entry in backstack
changeFragment(new YourFragmentClassName());
I am trying to talk to the fragments from activity.
Here in my MainActivity I am adding multiple fragments ok so for fine.
My main requirement is I don't want to add if fragment is already added.
So how can we check this condition?
Please help me some one.
code:-
private void intializingFragments(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
private View.OnClickListener intialization() {
return new View.OnClickListener() {
#Override
public void onClick(View v) {
int getId = v.getId();
if (getId == R.id.first) {
intializingFragments(new Fragment1());
} else if (getId == R.id.second) {
intializingFragments(new Fragment2());
} else if (getId == R.id.third) {
intializingFragments(new Fragment3());
}
}
};
}
You can use findFragmentByTag() or findFragmentById() functions to get a fragment. If mentioned methods are returning null then that fragment does not exist.
Fragment fragmentA = fragmentManager.findFragmentByTag("frag1");
if (fragmentA == null) {
// not exist
} else {
// fragment exist
}
for example:- http://wiki.workassis.com/android-load-two-fragments-in-one-framelayout/
You may find fragment in fragmentmanager:
List<Fragment> frags = getSupportFragmentManager().getFragments();
for (Fragment f : frags) {
<find what you want>...
}
Or you may add fragment with tag:
getSupportFragmentManager()
.beginTransaction()
.add(R.id.frame, new MyFragment(), "frag1")
.commit();
And find by tag
getSupportFragmentManager().findFragmentByTag("frag1");
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
Fragment topFragment = fragmentManager.findFragmentById(R.id.container);
FragmentA fragmentA = new FragmentA();
if(topFragment!= null)
{
transaction.remove(topFragment);
transaction.add(R.id.container, fragmentA, "FA");
transaction.commit();
}
else
{
transaction.add(R.id.container, fragmentA, "FA");
transaction.commit();
}
try this
private void intializingFragments(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment topFragment = fragmentManager.findFragmentById(R.id.container);
if(topFragment!= null)
{
fragmentTransaction.remove(topFragment);
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
else
{
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
How to check whether a fragment exists, without knowing its resource ID or tag:
// MyActivity.kt
val exists: Boolean = supportFragmentManager
.fragments
.filterIsInstance<MyFragment>()
.isNotEmpty()
There are many ways by which you can track the last added fragment. The simplest one is by finding the tag within fragment manager. Here is the sample code of it:
public boolean isFragmentPresent(String tag) {
Fragment frag = getSupportFragmentManager().findFragmentByTag(tag);
if (frag instanceof HomeFragment) {
return true;
} else
return false;
}
Alternatively, you can use your own variable to check whether the last added fragment is the same as the current fragment. Here is the sample code for it:
public boolean isCurrentFragment(Fragment fragment) {
if (fragment instanceof HomeFragment && getLastAddedFragment() instanceof HomeFragment) { // getLastAddedFragment() is a method which return the last added fragment instance
return true;
} else
return false;
}
And you can use it like:
if (isCurrentFragment(new HomeFragment())) {
// Last added Fragment is the HomeFragment
}
You can go for popBackStack Pop the last fragment transition from the manager's fragment back stack. If there is nothing to pop, false is returned. enter link description here
You can get the list of previously added fragments from the fragment manager. Then just check whether the list contains your fragment object or not.
getSupportFragmentManager().getFragments().contains(yourFragment);
This one doesn't work
!getSupportFragmentManager().getFragments().contains(fragment)
This works for me
getSupportFragmentManager().findFragmentByTag(fragment::class.java.getSimpleName()) == null
I know this question has been asked so many times but I have tried every thing which was answered
like
getSupportFragmentManager().executePendingTransactions();
used fragmentTransaction.replace
my problem is when I use findFragmentByTag, it works at one place and does not work at other place.
I am not getting where I am doing wrong because findFragmentByTag return fragment in R.id.action_clean case but does not work in R.id.action_cart case.
my code is
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.action_cart: // don't work in this case
if (Cart.getProductCount(this) > 0) {
if (getSupportFragmentManager().findFragmentByTag(CartFragment.class.getName()) != null) {
if (getSupportFragmentManager().findFragmentByTag(CartFragment.class.getName()).isVisible()) {
Log.v("visible","visible");
}
Log.v("visible2","not null");
Toast.makeText(this, "Cart is not empty", Toast.LENGTH_SHORT).show();
}
else{
changeFragment(new CartFragment());
}
}else{
Toast.makeText(this,"Cart is empty",Toast.LENGTH_SHORT).show();
}
break;
case R.id.action_login:
changeFragment(new LoginFragment());
// Toast.makeText(this,"login",Toast.LENGTH_SHORT).show();
break;
case R.id.action_signup:
changeFragment(new UserDetailFragment());
//Toast.makeText(this,"sign up",Toast.LENGTH_SHORT).show();
break;
case R.id.action_clean: // work here
if(Cart.clearCart(this)){
Toast.makeText(this,"All items has been removed from Cart",Toast.LENGTH_SHORT).show();
updateCartCount();
if (getSupportFragmentManager().findFragmentByTag(CartFragment.class.getName()) != null
&& getSupportFragmentManager().findFragmentByTag(CartFragment.class.getName()).isVisible()) {
removeFragment(getSupportFragmentManager().findFragmentByTag(CartFragment.class.getName()));
}
}else Toast.makeText(this,"Cart can not be cleaned",Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
and here is the changeFragment
#Override
public void changeFragment(Fragment fragment, CustomDictionary... dictionary) {
FragmentTransaction ft= getSupportFragmentManager().beginTransaction();
Bundle bundle = new Bundle();
if (dictionary != null && dictionary.length > 0) {
for (CustomDictionary dic : dictionary) {
bundle.putString(dic.getKey(),dic.getValue());
}
}
fragment.setArguments(bundle);
ft.setCustomAnimations(R.anim.enter, R.anim.exit);
ft.replace(R.id.fragmentContainer, fragment,fragment.getClass().getName()) // tag
.addToBackStack(fragment.getClass().getName())
.commit();
getSupportFragmentManager().executePendingTransactions();
}
removeFragment()
#Override
public void removeFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().remove(fragment)
.commit();
fragmentManager.popBackStack();
}
You used different name for tags...
you assigned tag by
fragment.getClass().getSimpleName()
And call it by
CartFragment.class.getName()
So just use one of them for both process setting and getting by tag.
P.S. You set tag when you call addToBackStack !
----- Update:
I would suggest to iterate all fragments you have in backstack by:
for (Fragment fragment : getFragmentManager().getFragments()) {
try {
Log.v(TAG,"fragment:"+fragment.getTag()+", isVisible:"+fragment.isVisible());
} catch (NullPointerException e) {
}
}
Then it will be clear what is the tag of fragment to call if exists :-)
use getSupportFragmentManager instead of getFragmentManager when call from activity.
I am using a navigation drawer.So when my application starts i am calling the homeFragment and keeping it in the backstack. Now if the user selects any options from the navigation drawer i am opening the respective fragment but without adding them to backstack. So what i want is that even when user has open 10 fragment, on pressing back they should be taken back to homeFragment only. But with my code the app exits on pressing back even homeFragment is in the backstack.
Code to openFragments
public static void replaceFragment(FragmentActivity activity, Fragment fragment, boolean addToBackStack) {
try {
String backStateName = fragment.getClass().getName();
String fragmentTag = backStateName;
FragmentManager manager = activity.getSupportFragmentManager();
boolean fragmentPopped = manager.popBackStackImmediate(fragmentTag, 0);
if (!fragmentPopped && manager.findFragmentByTag(fragmentTag) == null) {
FragmentTransaction ft = manager.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right);
ft.replace(R.id.frag_container, fragment, fragmentTag);
if (addToBackStack) {
ft.addToBackStack(backStateName);
}
ft.commit();
}
} catch (Exception e) {
e.printStackTrace();
}
}
From MainActivity
private void init() {
setUpToolBar();
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerFrag = (NavigationDrawerFrag) getSupportFragmentManager().findFragmentById(R.id.frag_nav_drawer);
drawerFrag.setUp(drawerLayout, toolbar, R.id.frag_nav_drawer);
CommonFunctions.replaceFragment(this, new HomeFrag(), true);
}
On NavigationDrawer Item click
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 1) {
CommonFunctions.replaceFragment(getActivity(), new ProfileFrag(), false);
} else if (position == 2) {
CommonFunctions.replaceFragment(getActivity(), new CelebrityFrag(), false);
} else if (position == 4) {
CommonFunctions.replaceFragment(getActivity(), new AboutUsFrag(), false);
} else if (position == 5) {
CommonFunctions.replaceFragment(getActivity(), new TermsAndConditions(), false);
}
lvNavItems.setItemChecked(position, true);
//lvNavItems.setSelection(position);
lvNavItems.setSelected(true);
mDrawerLayout.closeDrawer(fragContainer);
}
Add onBackPressed() method in activity to check backstack and if entry is more then zero so call popbackstack method.
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() != 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
//Put this in your onNavigationItemSelected of MainActivity
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
//FrameLayout where you load your home fragment tools:layout="#layout/frag_home"
ft.replace(R.id.content_frame,nav_selected);
if(getSupportFragmentManager().getBackStackEntryCount() != 0){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
getSupportFragmentManager().popBackStack();
}
//Don't add Home screen
if(id != R.id.nav_home){
ft.addToBackStack(null);
}
ft.commit();