0

I have just created a new Android project, and now Im trying to add a fragment as said here

The problem: after pressing back on my phone, getSupportFragmentManager().getBackStackEntryCount() returns zero so the doesn't return to the main activity, why?

I have followed the possible solutions here, but they didn't work for me.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            setContentView(R.layout.fragment_blank);


            // Check that the activity is using the layout version with
            // the fragment_container FrameLayout
            if (findViewById(R.id.fragment_container) != null) {

                // Create a new Fragment to be placed in the activity layout
                BlankFragment firstFragment = new BlankFragment();

                // In case this activity was started with special instructions from an
                // Intent, pass the Intent's extras to the fragment as arguments
                firstFragment.setArguments(getIntent().getExtras());

                // Add the fragment to the 'fragment_container' FrameLayout
                getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).
                        addToBackStack(null).commit();

                getSupportFragmentManager().executePendingTransactions();
            }
        }
    });
}

@Override
public void onBackPressed() {
    if(getSupportFragmentManager().getBackStackEntryCount() != 0) {
        getSupportFragmentManager().popBackStack();
    } else {
        super.onBackPressed();  //THIS IS CALLED!!
    }
}

EDIT 1: I want to add that when "Go back" after opening the fragment, I get the desktop of my mobile phone. Then, I can find the application if I press the "Recent applications" buttons, showing the content of the fragment, like this:

enter image description here

and then, if I choose it I get the app as the "Go back" button was pressed.

enter image description here

EDIT 2: Here is the whole class:

    package com.example.tirengarfio.myapplication;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    final FragmentManager fragmentManager = getSupportFragmentManager();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);



        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                setContentView(R.layout.fragment_blank);

                // Check that the activity is using the layout version with
                // the fragment_container FrameLayout
                if (findViewById(R.id.fragment_container) != null) {

                    // Create a new Fragment to be placed in the activity layout
                    BlankFragment firstFragment = new BlankFragment();

                    // In case this activity was started with special instructions from an
                    // Intent, pass the Intent's extras to the fragment as arguments
                    firstFragment.setArguments(getIntent().getExtras());

                    String foo = "foo";

                    // Add the fragment to the 'fragment_container' FrameLayout
                    getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).
                            addToBackStack(foo).commit();

                    getSupportFragmentManager().executePendingTransactions();
                }


            }
        });
    }

    @Override
    public void onBackPressed() {



        if(getSupportFragmentManager().getBackStackEntryCount() != 0) {
            getSupportFragmentManager().popBackStack();
        } else {
            super.onBackPressed();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}
Community
  • 1
  • 1
tirenweb
  • 30,963
  • 73
  • 183
  • 303
  • @FrankN.Stein could you explain your comment please? I'm using `addToBackStack` so in that way I think I'm adding the fragment to the BackStack, I can not find anything to solve my problem in your link's content. – tirenweb Dec 24 '15 at 14:47
  • @FrankN.Stein I have added a string but the result is the same.. – tirenweb Dec 24 '15 at 15:06
  • @FrankN.Stein I edited my question, maybe you find it useful to help me. – tirenweb Dec 24 '15 at 15:26
  • I see that it is class Activity, so getSupportFragmentManager().popBackStack() doesn't run. – Quang Doan Dec 24 '15 at 15:32
  • @QuangDoan but I'm using `AppCompatActivity` in my code. Im going to paste the whole class now in my question. – tirenweb Dec 24 '15 at 15:35
  • popBackStack() is called when you want from FragmentB return FragmentA. Fragment A and B are in the one Activity – Quang Doan Dec 24 '15 at 15:52

0 Answers0