在backstack中添加新片段时,onBackStackChanged()调用两次

时间:2017-03-21 20:56:51

标签: java android android-fragments android-view fragmentmanager

所以,我注意到有趣的问题。当我以编程方式添加新片段时,OnBackStackChangedListener中的onBackStackChanged方法被调用两次,但它必须只调用一次。这是我的活动代码:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements FragmentManager.OnBackStackChangedListener{

    private FragmentManager fragmentManager;
    private Button button1;
    private Button button2;
    private Button button3;

    private Fragment defaultFragment;
    private Fragment previousFragment;
    private Fragment currentFragment;
    private String currentFragmentTag;

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

        button1 = (Button) findViewById(R.id.button);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showFragment(new FragmentOne(), FragmentOne.TAG);
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showFragment(new FragmentTwo(), FragmentTwo.TAG);
            }
        });

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showFragment(new FragmentThree(), FragmentThree.TAG);
            }
        });

        setupFragmentManager();

    }

    private void setupFragmentManager() {
        fragmentManager = getSupportFragmentManager();
        fragmentManager.addOnBackStackChangedListener(this);


        FragmentOne fragmentOne = new FragmentOne();
        defaultFragment = fragmentOne;  // fragment for default

        currentFragmentTag = FragmentOne.TAG;
        currentFragment = fragmentOne;

        fragmentManager.beginTransaction()
                .add(R.id.fragments_container, currentFragment, currentFragmentTag)
                .addToBackStack(currentFragmentTag)
                .commit();
    }

    public void showFragment(Fragment fragment, String fragmentTag) {
        previousFragment = currentFragment;
        currentFragment = fragment;
        fragmentManager.beginTransaction()
                .hide(previousFragment)
                .add(R.id.fragments_container, fragment, fragmentTag)
                .addToBackStack(fragmentTag)
                .commit();
    }

    @Override
    public void onBackStackChanged() {
        Log.d("MY_TAG" , "onBackStackChangedListener - " + fragmentManager.getBackStackEntryCount() );

        if (fragmentManager.getBackStackEntryCount() > 0) {    
            String fragmentTag = fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount() - 1).getName();
            currentFragment = fragmentManager.findFragmentByTag(fragmentTag);
        } else {
            currentFragment = defaultFragment;
        }

// some code...
    }

    @Override
    public void onBackPressed() {
        if (fragmentManager.getBackStackEntryCount() == 1) { 
            finish();
        } else {
            super.onBackPressed();
        }
    }
}

片段类:

public class FragmentOne extends Fragment {

    public static final String TAG = FragmentOne.class.getSimpleName();

    private Context context;

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


        context = getActivity();
        View topLevelView = LayoutInflater.from(context).inflate(R.layout.fragment_layout, container, false);

        TextView textView = (TextView) topLevelView.findViewById(R.id.fragment_Text);

        textView.setText("This is FRAGMENT ONE");

        return topLevelView;
    }

}

FragmentTwo和FragmentThree与FragmentOne相同。

当我启动我的应用程序时,在日志中我看到:

onBackStackChangedListener - 1
onBackStackChangedListener - 1

按下按钮(显示下一个片段)后,我看到:

onBackStackChangedListener - 2
onBackStackChangedListener - 2

另一个:

onBackStackChangedListener - 3
onBackStackChangedListener - 3

改变

.add(R.id.fragments_container, currentFragment, currentFragmentTag)

.replace(R.id.fragments_container, currentFragment, currentFragmentTag)

无法解决问题。

但这很奇怪。为什么我的OnBackStackChangedListener在添加片段后调用了多次自己的方法?

2 个答案:

答案 0 :(得分:11)

无法相信它会发生。创建了一个类似的项目并潜入调试器。结果证明它是新支持库中的一个错误。这是bug opened in the tracker

切换回25.0.0将按预期工作。

compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:support-v4:25.0.0'

虽然在release notes中未提及,但提及in the issue tracker,但问题已在25.4.0中修复,这很好。

答案 1 :(得分:2)

只是一个更新。谷歌最终发布了25.4.0版本,修复了该问题。

只是,现在支持的东西是从Google的Maven存储库获取的:

maven { url 'https://maven.google.com' }

因此,您可以开始使用所有内容的新版本,并从SDK Manager中卸载本地支持存储库。