我如何获得后退按钮转到水平滚动一定的片段?退出现在应用程式 [英] How do I get the back button to go to a certain Fragment in horizontal scrolling? Exits app right now

查看:112
本文介绍了我如何获得后退按钮转到水平滚动一定的片段?退出现在应用程式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题pretty多解释了。

the title pretty much explains it.

我已经横向滚动成立,第一屏有按键的其他片段以及整个水平滚动系统。我想是用户能够preSS后退按钮在这些片段中的一个,为应用程序时,返回到第一屏与所有的按钮。

I have horizontal scrolling set up, the first screen has buttons to the other Fragments as well as the whole horizontal scroll system. What I would like is for the user to be able to press the back button when on one of these fragments and for the app to return to the first screen with all the buttons.

从那里,我想后退按钮是一个AlertDialog询问用户是否愿意退出应用程序。目前,这是正在发生的事情(在所有的碎片,当你preSS后退按钮的AlertDialog我创建了持久性有机污染物)。

From there I want the back button to be an AlertDialog asking the user if they would like to exit the app. At the moment this is what is happening (On all Fragments when you press the back button the AlertDialog I created pops up).

我看着片段交易和addToBackStack(),但我不知道如何实现它。我看了看开发者指南和某些问题上这个网站,但得到code的一行或两行不执行它帮助。

I've looked into Fragment transactions and "addToBackStack()" but I don't know how to implement it. I've looked at the dev guide and certain questions on this site but getting one or two lines of code doesn't help in implementing it.

我有建立一个FragmentPagerAdapter一个FragmentActivity,每个片段都有自己的Java文件。我有5个片段,它们都堪称FragmentActivity和FragmentPagerAdapter。

I have a FragmentActivity with a FragmentPagerAdapter set up and each Fragment has its own Java file. I have 5 Fragments that are all called in the FragmentActivity and FragmentPagerAdapter.

我不认为我需要告诉你的家伙我的任何code的的时刻,因为这一切都在正常的方式成立。请让我知道如果你这样做,虽然。

I don't think I need to show you guys any of my code for the moment since it's all set up in the normal manner. Please let me know if you do though.

code的我在其他的问题,一个特别的发现是位如下:

The bit of code I found on other questions and one in particular was the following:

FragmentTransaction tx = fragmentManager.beginTransation();
tx.replace( R.id.fragment, new MyFragment() ).addToBackStack( "tag" ).commit();

这是一个有点难以只是,虽然下去了。

It's a bit hard to go on just that though.

我真的AP preciate你的帮助。

I would really appreciate your help.

编辑:打消了我的code - 是没有必要

my code removed - wasn't needed.

推荐答案

如果您使用 ViewPager 从你的问题我刚才回答了,你想回来的第一片段 ViewPager 当用户presses那么返回按钮覆盖 onBack pressed 的方法是这样的:

If you use the ViewPager from your question which I answered earlier and you want to come back to the first fragment of the ViewPager when the user presses the BACK button then override the onBackPressed method like this:

@Override
public void onBackPressed() {
    if (getSupportFragmentManager().findFragmentByTag("outDialog") != null
            && ((DialogFragment) getSupportFragmentManager()
                    .findFragmentByTag("outDialog")).isVisible()) {
        // we have the out dialog visible and the user clicked back so let
        // the
        // normal events happen
        super.onBackPressed();
        return;
    }
    int currentPosition = mViewPager.getCurrentItem();
    if (currentPosition != 0) {
        // if the page the ViewPager shows isn't the first one then move it
        // to the first one
        mViewPager.setCurrentItem(0);
    } else {
        // we are at the first position already and the user wants out, so
        // annoy him with a dialog that asks him once again if he wants out.
        DialogFragment askHim = new DialogFragment();
        askHim.show(getSupportFragmentManager(), "outDialog");
        // in the dialog listener, if the user presses ok, finish the activity
    }
}

这篇关于我如何获得后退按钮转到水平滚动一定的片段?退出现在应用程式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆