Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to go back to previous fragment from activity?

I've got an app with nav drawer, which is switching fragments. From inside one of those fragments, I am calling a new activity. When I click back in this activity (in toolbar), I want to go back to previous selected fragment but instead it puts me back to first fragment. I am adding my fragment to back stack so this should not be a problem.

Here is what I have already tried:

I have overriden the onBackPressed method in my 2nd activity like this:

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

It is not working. I also saved index of current fragment inside onsaveinstancestate method and retrieve it but same result. I also tried with always putting current fragment inside variable and try to reshow it but still, it does not work. Anything else I could try?

Fun fact: if I press back inside bottom panel, it does actually goes back to previous fragment.

EDIT: Here is my code for doing this:

 private void replaceFragment(Fragment fragment)
 {
     if (fragment != null)
     {
         FragmentManager manager = getSupportFragmentManager();
         manager.beginTransaction()
                    .replace(R.id.main_content, fragment)
                    .addToBackStack(null)
                    .commit();
        }
    }

I add first fragment only, if savedInstanceState is null, like so:

if (savedInstanceState == null) {
    // first time
    mTitle = getResources().getString(R.string.home);
    replaceFragment(HomeFragment.newInstance()); 
}

And yes, all this is done inside onCreate() method.

like image 814
MaTTo Avatar asked Jul 18 '15 12:07

MaTTo


2 Answers

I had the same problem and got fixed. The only thing you need to do is to override the method "onOptionsItemSelected" in the activity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        finish();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

android.R.id.home is your first fragment in the menu.

like image 128
L.L. Avatar answered Sep 19 '22 17:09

L.L.


What I tend to do is this

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() == 0) {
        this.finish();
    } else {
        super.onBackPressed(); //replaced
    }
}

This way it handles the fragment stuff on its own within, but when there's no fragments left to go back to, then it finishes the activity.

EDIT: UP navigation can recreate your previous activity even if it already exists. To prevent that from happening, redefine the Up navigation's event in onOptionsItemSelected like so:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent parentIntent = NavUtils.getParentActivityIntent(this);
            if(parentIntent == null) { 
                finish();
                return true;
            } else {
                parentIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(parentIntent);
                finish();
                return true;
            }
    }
    return super.onOptionsItemSelected(item);
}
like image 33
EpicPandaForce Avatar answered Sep 21 '22 17:09

EpicPandaForce