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

时间:2015-12-24 14:30:30

标签: android android-fragments

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

问题:按下我的手机后,getSupportFragmentManager().getBackStackEntryCount()返回零,所以不会返回主要活动,为什么?

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

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

enter image description here

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

enter image description here

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

}

0 个答案:

没有答案
相关问题