使用onBack pressed()在Android的碎片碎片、onBack、pressed、Android

2023-09-13 00:43:17 作者:自认与酒同醉

我工作的一个项目,我需要能够使用在每个片段中的后退按钮previous片段之间导航,我有写操作栏中使用后退箭头这样做的方法,但是,我希望能够使用上的后退按钮pressed相同的功能。我不希望使用背面栈。有没有办法做到这一点?

I am working on a project and I need to be able to use the back button in each fragment to navigate between previous fragments, I have methods written to do so by using a back arrow in the action bar, however, I want to be able to use the same functionality on the back button pressed. I don't want to use the back stack. Is there a way to do this?

修改

而不是使用回栈我希望能够调用返回到previous方法,当用户单击后退按钮下方。我需要的片段中使用的GoBack的pressed方法。这可能吗?我希望这是明确和简洁。道歉的任何困惑上面所引起的。

Rather than using the back stack I want to be able to call the go back to previous method below when the user clicks the back button. I need to used the gobackpressed method within fragments. Is this possible? I hope this is clear and concise. Apologies for any confusion caused above.

返回到previous

Go Back to Previous

public void gobackToPreviousFragment(String preFragmentTag, Fragment preFragment){

    FragmentManager  fm = getSupportFragmentManager();

    FragmentTransaction ft = fm.beginTransaction();
    ft.setCustomAnimations(R.animator.close_slide_in,R.animator.close_slide_out);

    ft.show(preFragment);

    //**BY REMOVING FRAGMENT, WHEN USER TRIES TO REVISIT, FRAGMENT IS BLACK**

    ft.remove(fm.findFragmentByTag(Misc.currentContentFragmentTag));
    ft.addToBackStack(null);
    ft.commit();

    Misc.currentContentFragmentTag = preFragmentTag;

    createBar(preFragment);
}

往前走

public void gotoNextFragment(String nextTag, Fragment nextFragment){

    FragmentManager  fm = getSupportFragmentManager();

    FragmentTransaction ft = fm.beginTransaction();
    ft.setCustomAnimations(R.animator.enter_slide_in, R.animator.enter_slide_out);

    boolean newlyCreated = false;
    if(nextFragment == null){
        nextFragment = Fragment.instantiate(this, nextTag);
        newlyCreated = true;
    }

    //hide current fragment
    ft.hide(fm.findFragmentByTag(Misc.currentContentFragmentTag));

    if(newlyCreated){
        ft.add(R.id.content_frame, nextFragment, nextTag);
    }
    else{
        ft.show(nextFragment);
    }

    ft.addToBackStack(null);
    ft.commit();
    Misc.currentContentFragmentTag = nextTag;

    createBar(nextFragment);
}

这些都是我如何浏览来回,我希望能够实现对onBack pressed了回去()方法。这是否有意义?

These are how I navigate back and forth, and I'd like to be able to implement the go back method on the onBackPressed(). Does this make sense?

推荐答案

为什么你不希望使用回栈?如果有一个潜在的问题或困惑也许我们可以把它清除掉你。

Why don't you want to use the back stack? If there is an underlying problem or confusion maybe we can clear it up for you.

如果你想坚持你的要求,只需覆盖您的活动的onBack pressed()方法,并调用任何方法你打电话时,你的动作条后退箭头被点击。

If you want to stick with your requirement just override your Activity's onBackPressed() method and call whatever method you're calling when the back arrow in your ActionBar gets clicked.

编辑:如何解决黑屏片段回栈的问题:

How to solve the "black screen" fragment back stack problem:

您可以通过添加一个backstack监听到的片段管理解决这个问题。这侦听器检查,如果该片段回栈是空的,并完成相应的活动:

You can get around that issue by adding a backstack listener to the fragment manager. That listener checks if the fragment back stack is empty and finishes the Activity accordingly:

您可以设置监听器在活动的onCreate方法:

You can set that listener in your Activity's onCreate method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FragmentManager fm = getFragmentManager();
    fm.addOnBackStackChangedListener(new OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            if(getFragmentManager().getBackStackEntryCount() == 0) finish();
        }
    });
}