Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to end a Fragment

In my application I have a navigation drawer, and the way my app works is there is only one activity and if you select something from the Navigation drawer it starts/replaces the current fragment. How Can I make it so that when something is selected from the navigation drawer and a new fragment starts the old fragment ends

I cant do

@Override
public void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    getActivity().finish();
}   

because that would kill the app since there is only one activity.

How do I kill the fragment?

EDIT: my fragment transaction

Home HomeFragment = new Home();
        transaction.replace(R.id.llhome, HomeFragment);
        transaction.addToBackStack(null);
        transaction.commit();
              mDrawerList.setItemChecked(position, true);
                mDrawerLayout.closeDrawer(mDrawerList);

thanks

like image 523
user222786 Avatar asked Jun 20 '13 20:06

user222786


2 Answers

Just call:

getActivity().onBackPressed();
like image 108
Chetan Gaikwad Avatar answered Oct 02 '22 06:10

Chetan Gaikwad


If you are using dynamic fragments, set up via a FragmentTransaction, you could run a transaction to replace() the old fragment with the new one.

like image 42
CommonsWare Avatar answered Oct 02 '22 04:10

CommonsWare