Android 安卓:添加片段后,`getSupportFragmentManager()。GetBackBackbackEntryCount()`返回零

Android 安卓:添加片段后,`getSupportFragmentManager()。GetBackBackbackEntryCount()`返回零,android,android-fragments,Android,Android Fragments,我刚刚创建了一个新的Android项目,现在我正试图添加一个片段 问题:在按下手机上的“后退”按钮后,getSupportFragmentManager().getBackbackEntryCount()返回零,因此不会返回到主活动,为什么 我遵循了可能的解决方案,但它们对我不起作用 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

我刚刚创建了一个新的Android项目,现在我正试图添加一个片段

问题:在按下手机上的“后退”按钮后,
getSupportFragmentManager().getBackbackEntryCount()
返回零,因此不会返回到主活动,为什么

我遵循了可能的解决方案,但它们对我不起作用

@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!!
    }
}
编辑1:我想补充一点,打开片段后“返回”时,我会看到我手机的桌面。然后,如果我按下“最近的应用程序”按钮,可以找到应用程序,显示片段的内容,如下所示:

然后,如果我选择它,我会在按下“返回”按钮时得到应用程序

编辑2:以下是整个课程:

    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);
    }

}

@你能解释一下你的评论吗?我使用的是
addToBackStack
,因此我认为我正在将片段添加到BackStack中,我无法在您的链接内容中找到任何解决我的问题的方法。@FrankN.Stein我添加了一个字符串,但结果是相同的。@FrankN.Stein我编辑了我的问题,也许您觉得它对我有帮助。我发现这是课堂活动,因此getSupportFragmentManager().popBackStack()不运行。@QuangDoan,但我在代码中使用了
AppCompatActivity
。我现在要把全班同学都贴在我的问题上。