Prerna Singhal
Prerna Singhal

Reputation: 153

Back Button on fragment not working

I have a fragment activity from where i can start my fragments which contain a viewpager. In my fragment activity I have added this piece of code.

fragment = new ItemPagerFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.content_frame, fragment);
transaction.addToBackStack(null);
transaction.commit();

Now when I press back button only blank screen appears and it does not lead me to my fragment activity. What wrong i might be doing?

Upvotes: 3

Views: 2490

Answers (5)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75778

Try this way,

                   // Create new fragment and transaction
                    Fragment newFragment = new ItemPagerFragment();
                    // consider using Java coding conventions (upper first char class names!!!)
                    FragmentTransaction transaction = getFragmentManager().beginTransaction();
                    // Replace whatever is in the fragment_container view with this fragment,
                    // and add the transaction to the back stack
                    transaction.replace(R.id.content_frame, newFragment);
                    transaction.addToBackStack(null);
                    // Commit the transaction
                    transaction.commit();

Modify your code

    @Override
public void onBackPressed() {
    if(some condition) {
        // do something
    } else {
        super.onBackPressed();
    }
}

Please follow this link Handling back button press Inside Fragments

Upvotes: 1

Vishwa
Vishwa

Reputation: 1112

While launching fragment assign a TAG to each fragment.When you want to back press check the existence of an fragment using assigned TAG name and then do corresponding action.for example:

private final static String TAG_FRAGMENT = "TAG_FRAGMENT";
//when want to add fragment to view
private void showFragment() {
    final Myfragment fragment = new MyFragment();
    final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment, fragment, TAG_FRAGMENT);
    transaction.addToBackStack(null);
    transaction.commit();
}
//in activity back pressed check the existence of fragment by TAG
@Override
public void onBackPressed() {
    final Myfragment fragment = (Myfragment) getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);

    if (fragment.allowBackPressed()) { // and then you define a method allowBackPressed with the logic to allow back pressed or not
        super.onBackPressed();
    }
}

Upvotes: 0

vinita jain
vinita jain

Reputation: 183

private void changeFragment (Fragment fragment){


  String backStackName = fragment.getClass().getName();

  FragmentManager manager = getSupportFragmentManager();
  boolean fragmentPopped = manager.popBackStackImmediate (backStackName, 0);

  if (!fragmentPopped){ //fragment not in back stack, create it.
    FragmentTransaction ft = manager.beginTransaction();
    ft.replace(R.id.content_frame, fragment);
    ft.addToBackStack(backStackName );
    ft.commit();
  }
}

Upvotes: 0

Shadow
Shadow

Reputation: 6899

Use this in activity that holds fragment.

@Override
public void onBackPressed() {
    if (fragmentManager.getBackStackEntryCount() > 1) {
        fragmentManager.popBackStack();
    } else
        finish();
}

Upvotes: 2

Arturo
Arturo

Reputation: 548

You have to tell to the fragment manager that the back button was pressed. Override onBackPressed on your Activity

@Override
public void onBackPressed() {
   if (getFragmentManager().getBackStackEntryCount() == 0) {
      super.onBackPressed();      
   } else {
      getFragmentManager().popBackStack();
   }
}

Upvotes: 0

Related Questions