1
votes

I have one fragment in an activity, I can launch the fragment using FragmentManager and FragmentTransaction, but when I try to navigate back to the Activity via the back button, the app exits.

Here is my fragment creation & transaction in the Activity class:

DetailFragment df = new DetailFragment();

try {
    df.initFragment(jsonObject);
} catch (Exception e) {
    e.printStackTrace();
}

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

fragmentTransaction.replace(R.id.fragment_container, df);
fragmentTransaction.addToBackStack(null);

fragmentTransaction.commit();

I also override onBackPressed()

@Override
public void onBackPressed() {
    super.onBackPressed();
}

With the above setup, one press of the back button exits to the home screen.


I also tried to modify onBackPressed() to do:

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

On this second version of onBackPressed(), it takes 2 back presses to go back to the home screen. Keep in mind I am trying to return to the original Activity.

Any suggestions?

P.S. I can add my fragment code if needed

2
What is your activity class extending? i.e., ActionBarActivity, Activity, etc? - RogueBaneling
@RogueBaneling activity class extends AppCompatActivity - Davis Allen

2 Answers

0
votes

Your second version of onBackPressed() requires 2 back presses to go back to the home screen. So it seems like fm.popBackStack() is getting called. Try replacing it with popBackStackImmediate().

Edit: When you are creating the fragment, try using the following instead of .replace:

fragmentTransaction.add(R.id.fragment_container, df);

If that still doesn't work, try:

@Override
public void onBackPressed(){
    FragmentManager fm = getFragmentManager();
    if (df.isVisible) {
        fm.beginTransaction().remove(df).commit();
    } else {
        super.onBackPressed();  
    }
}

(although this is less desirable as you are hard-coding what to remove).

0
votes

The navigation was working, but in my Fragment's onCreateView I used setContentView() to the fragment view which painted the fragment view to the main activity, so it appeared that the back button would exit out if it was pressed twice, but it was actually returning to the main activity as it should, it just repainted the main activity so it wasnt possible to tell.

Thanks for the help @RogueBaneling !!

original code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View fragmentView = inflater.inflate(R.layout.fragment_detail, container, false);
    fragmentView.setClickable(true);
    getActivity().setContentView(R.layout.fragment_detail);


    final TextView movieTitleText = (TextView) getActivity().findViewById(R.id.movie_title_text);
    movieTitleText.setText(title);

    // editing textView code... 

    return fragmentView;
}

into:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View fragmentView = inflater.inflate(R.layout.fragment_detail, container, false);
    fragmentView.setClickable(true);


    final TextView movieTitleText = (TextView) fragmentView.findViewById(R.id.movie_title_text);
    movieTitleText.setText(title);

    // editing textView code... 

    return fragmentView;
}