从片段到另一个片段的后退按钮

时间:2015-06-11 09:36:36

标签: android android-studio fragment back-button

当我按下Back Button时我的应用程序正在关闭,所以我做了一些关于它的研究,我发现: How to handle back button in activityHow to catch device back button event in android?

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK ) {
        //do your stuff
    }
    return super.onKeyDown(keyCode, event);
}

但我明白了:"无法解决方法onKeyDown ..."

我的片段:

public class NosOffres extends android.support.v4.app.Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_nosoffres, container, false);
        TextView firstTitle = (TextView) view.findViewById(R.id.firstTitle);
        firstTitle.setText(Html.fromHtml("<b>" + "<font color=#263355>RACHAT DE PR&Ecirc;TS IMMOBILIER</font>" + "</b>"));
        firstTitle.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/GothamBook.ttf"));
        TextView firstText = (TextView) view.findViewById(R.id.firstText);
        firstText.setText(Html.fromHtml("<font color=#5D5D5C>Op&eacute;ration qui permet de rassembler des cr&eacute;dits immobilier et des cr&eacute;dits &agrave; la consommation sur un seul nouveau contrat incluant une nouvelle dur&eacute;e de remboursement et une mensualit&eacute; r&eacute;duite.<br/><br/>Le financement est immobilier lorsque la part des encours immobiliers &agrave; reprendre est sup&eacute;rieure &agrave; 60% par rapport au total des capitaux &agrave; reprendre.</font>"));
        firstText.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/GothamBook.ttf"));
        TextView firstDetail = (TextView) view.findViewById(R.id.firstDetail);
        firstDetail.setText(Html.fromHtml("<font color=#263355>Taux :" + "<b>" + " fixe ou r&eacute;visable" + "</b><br/>" +
                "Dur&eacute;es :" + "<b>" + " de 60 &agrave; 420 mois" + "</b><br/>" +
                "Assurance :" + "<b>" + " facultative</font>" + "</b>"));
        firstDetail.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/GothamBook.ttf"));

        TextView secondTitle = (TextView) view.findViewById(R.id.secondTitle);
        secondTitle.setText(Html.fromHtml("<b>" + "<font color=#263355>RACHAT DE PR&Ecirc;TS CONSOMMATION</font>" + "</b>"));
        secondTitle.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/GothamBook.ttf"));
        TextView secondText = (TextView) view.findViewById(R.id.secondText);
        secondText.setText(Html.fromHtml("<font color=#5D5D5C>Op&eacute;ration qui permet de rassembler des cr&eacute;dits &agrave; la consommation sur un seul nouveau contrat incluant une nouvelle dur&eacute;e de remboursement et une mensualit&eacute; r&eacute;duite.<br/><br/>Le financement est &agrave; la consommation lorsque la part des encours immobiliers &agrave; reprendre est inf&eacute;rieure &agrave; 60% par rapport au total des capitaux &agrave; reprendre.</font>"));
        secondText.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/GothamBook.ttf"));
        TextView secondDetail = (TextView) view.findViewById(R.id.secondDetail);
        secondDetail.setText(Html.fromHtml("<font color=#263355>Taux :" + "<b>" + " fixe ou r&eacute;visable" + "</b><br/>" +
                "Dur&eacute;es :" + "<b>" + " &agrave; partir de 12 mois" + "</b><br/>" +
                "Assurance :" + "<b>" + " facultative</font>" + "</b>"));
        secondDetail.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/GothamBook.ttf"));
        TextView textView =(TextView)getActivity().findViewById(R.id.main_toolbar_title);
        textView.setText(getString(R.string.title_nosoffres));
        textView.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/GothamBook.ttf"));
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ImageView formulaire = (ImageView) getView().findViewById(R.id.formulaire);
        formulaire.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.solutis.fr/demande-rachat-credit.html/#utm_source=googleplay&utm_medium=application&utm_campaign=application-solutis-android")));
            }
        });
    }
}

我不想进入这个片段:

public class Accueil extends android.support.v4.app.Fragment {

    //Lors de la creation de la vue, on va attribuer a chaque zone de texte, le texte voulu avec un type particulier (font family...)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //placer ici le code pour connaitre la densite et la resolution de lecran
        View view = inflater.inflate(R.layout.activity_accueil, container, false);
        TextView topTextL1 = (TextView) view.findViewById(R.id.topTextL1);
        topTextL1.setText(Html.fromHtml("<font color=#263355>VOTRE EXPERT DU</font>"));
        topTextL1.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/GothamBook.ttf"));
        TextView topTextL2 = (TextView) view.findViewById(R.id.topTextL2);
        topTextL2.setText(Html.fromHtml("<b>" + "<font color=#263355> REGROUPEMENT DE CR&Eacute;DITS </font>" + "</b>"));
        topTextL2.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/GothamBook.ttf"));
        TextView topTextL3 = (TextView) view.findViewById(R.id.topTextL3);
        topTextL3.setText(Html.fromHtml("<font color=#263355>EN FRANCE DEPUIS </font><font color=#EF7A05>1998</font><font color=#263355>.</font>"));
        topTextL3.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/GothamBook.ttf"));
        TextView bottomText = (TextView) view.findViewById(R.id.bottomText);
        bottomText.setText(Html.fromHtml("<font color=#5D5D5C>Un cr&eacute;dit vous engage et doit &ecirc;tre rembours&eacute;. V&eacute;rifiez vos capacit&eacute;s de remboursement avant de vous engager.</font>"));
        bottomText.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/GothamBook.ttf"));
        return view;
    }

    //Apres creation de la vue ont va creer les evenements
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ImageView nosoffres = (ImageView) getView().findViewById(R.id.firstBlock);
        nosoffres.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Fragment fragment = new NosOffres();
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.accueil, fragment);
                fragmentTransaction.commit();
            }
        });
        ImageView contact = (ImageView) getView().findViewById(R.id.secondBlock);
        contact.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Fragment fragment = new ContactezNous();
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.accueil, fragment);
                fragmentTransaction.commit();
            }
        });
        ImageView actualites = (ImageView) getView().findViewById(R.id.thirdBlock);
        actualites.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Fragment fragment = new Actualites();
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.accueil, fragment);
                fragmentTransaction.commit();
            }
        });
        ImageView mentions = (ImageView) getView().findViewById(R.id.fourthBlock);
        mentions.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Fragment fragment = new MentionsLegales();
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.accueil, fragment);
                fragmentTransaction.commit();
            }
        });
        ImageView formulaire = (ImageView) getView().findViewById(R.id.formulaire);
        formulaire.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.solutis.fr/demande-rachat-credit.html/#utm_source=googleplay&utm_medium=application&utm_campaign=application-solutis-android")));
            }
        });
    }
}

2 个答案:

答案 0 :(得分:3)

protected void Opdater_Click(object sender, EventArgs e) { ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "sometihg", "block();", true); Frontsite.DataSource = DL.GetWetmixBatches(calStart.SelectedDate, calStop.SelectedDate, null, null, null, null, null, null, null, null, null); Frontsite.DataBind(); System.Diagnostics.Debug.WriteLine("Have no clue"); ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "sometihg", "unblock();", true); }

@Override onBackPressed()方法

FragmentActivity

答案 1 :(得分:0)

创建静态片段对象 -

public static Fragment currentFragment = null;

并在Activity / FragmentActivity类中初始化它

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK ) {
        //do your stuff

        try{
            if(currentFragemnt!=null)
            {
                // do you work here respected to currentFragment
            }

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    currentFragemnt = null;
    return super.onKeyDown(keyCode, event);
}

因此基本活动将检测设备的backPressed并执行任务。片段无法检测到backPressed,只有它们所连接的活动才能检测到它。

将setOnClickListener更改为 -

nosoffres.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                currentFragment = new NosOffres();
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.accueil, fragment);
                fragmentTransaction.commit();
            }
        });