Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pressing back does not return to previous fragment

I have a problem with adding the fragment transactions to the back stack. I have a Main activity in which I populate my layout with a Menu Fragment:

public class MainActivity extends ActionBarActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getFragmentManager().beginTransaction().add(R.id.frag_container, new MainMenuFragment()).commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

Then, inside the MainMenuFragment, the user chooses some option which results in replacing the menu fragment with some other fragment:

public class MainMenuFragment extends Fragment implements OnItemClickListener{
    GridView grid;
    FragmentManager manager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.main_menu_fragment, container, false);

    manager = getActivity().getFragmentManager();
    grid = (GridView) root.findViewById(R.id.gridView1);

    grid.setAdapter(new MenuTileAdapter(getActivity()));
    grid.setOnItemClickListener(this);

    return root;
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    FragmentTransaction trans = manager.beginTransaction();
    if (position == 0){
        trans.replace(R.id.frag_container, new BasicSettingsFragment());
        trans.addToBackStack(null);
        trans.commit();
    }
}

}

For what i understand, this should make it so that when the user presses back button on their device, they will be brought back to the menu fragment, but instead this quits the app. What am i doing wrong?

like image 526
JamMaster Avatar asked Sep 25 '14 20:09

JamMaster


People also ask

How do I go back to a previous activity in fragment?

You can get a reference to the FragmentActivity by calling getActivity() on your current Fragment and then call from the Activity retrieved onBackPressed() method.

How do you ensure that the user can return to the previous fragment by pressing the back button?

Calling addToBackStack() commits the transaction to the back stack. The user can later reverse the transaction and bring back the previous fragment by pressing the Back button. If you added or removed multiple fragments within a single transaction, all of those operations are undone when the back stack is popped.

How do you stop a fragment from recreating?

Calling setRetainInstance(true) will prevent the Fragment from being re-created. But, right now the Activity is always re-creating the DrawerFragment and forcefully re-creating the HomeFragment , resulting in the behavior you are seeing. In your Activity. onCreate() check the savedInstanceState is null .

How do I return one fragment to another?

The title of the toolbar should change between the two fragments (from "First Fragment" to "Second Fragment"). The second fragment should have a "back button" (arrow left icon) in the toolbar that, on click, lets the user go back to the first fragment.


2 Answers

In your Activity overwrite:

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

And probably you need to use in every commited fragment transaction:

FragmentTransaction.addToBackStack(null);
like image 58
Adam Styrc Avatar answered Sep 27 '22 18:09

Adam Styrc


Your code is a mixup, you use ActionBarActivity from appcompat and not using getSupportFragmentManager() and the fragments import should be the appcompat one if you decide to use it. If not, use Activity instead of ActionBarActivity and the simple Fragment import with FragmentManager

Add this to your activity android:configChanges="keyboardHidden|orientation|screenSize" This will stop your activity from restarting when you rotate. use setRetainInstance(true) on fragments.

like image 32
Georgian Benetatos Avatar answered Sep 27 '22 17:09

Georgian Benetatos