简体   繁体   English

Android Fragment返回新闻

[英]Android Fragment Back press

I have a couple of fragments. 我有几个片段。 But all the fragments lead to PeriodFragment when they press a button. 但是,当所有片段按下按钮时,它们都将导致PeriodFragment。 My question is how do I implement so that when the user presses the BACK button on his mobile phone, the PeriodFragment will switch back to the fragment it was entered from. 我的问题是我应该如何实现,以便当用户按下手机上的BACK按钮时,PeriodFragment将切换回输入该片段的位置。 This is the java code from PeriodFragment.java: 这是PeriodFragment.java中的Java代码:

public class PeriodFragment extends Fragment {

    Button btnPretrazi;

    public PeriodFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_period, container, false);


        //buttonPretraziPeriod
        btnPretrazi = (Button) view.findViewById(R.id.buttonPretraziPeriod);

        btnPretrazi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(),"test", Toast.LENGTH_SHORT).show();
            }
        });


        return view;
    }

}

This is my TvrdjavaFragment.java (one of the fragments that have the button to switch to PeriodFragment.java) : 这是我的TvrdjavaFragment.java(具有按钮以切换到PeriodFragment.java的片段之一):

package com.example.ivanp.msnis;

public class TvrdjavaFragment extends Fragment {

    Button btnIdinaperiod;

    public TvrdjavaFragment() {
        // Required empty public constructor
    }


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

        View view = inflater.inflate(R.layout.fragment_tvrdjava, container, false);
        // Inflate the layout for this fragment
        //show date
        TextView datumprikaz = (TextView) view.findViewById(R.id.datumprikaz);
        Date danas = new Date();

        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        String novDatum = sdf.format(danas);

        datumprikaz.setText(novDatum);
        //End of show date

        btnIdinaperiod = (Button) view.findViewById(R.id.buttonIdinaperiod);

        btnIdinaperiod.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PeriodFragment periodFragment = new PeriodFragment();
                FragmentTransaction periodFragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
                periodFragmentTransaction.replace(R.id.frame, periodFragment);
                periodFragmentTransaction.commit();
            }
        });

        return view;
    }

}

I'm new to android studio, so please tell the details. 我是android studio的新手,所以请告诉细节。

According to android docs When using fragments in your app, individual FragmentTransaction objects may represent context changes that should be added to the back stack. 根据android docs,当在您的应用中使用片段时,各个FragmentTransaction对象可能表示应添加到后台堆栈的上下文更改。 For example, if you are implementing a master/detail flow on a handset by swapping out fragments, you should ensure that pressing the Back button on a detail screen returns the user to the master screen. 例如,如果要通过换出片段在手机上实现主/细节流程,则应确保在详细信息屏幕上按“后退”按钮可使用户返回到主屏幕。 To do so, call addToBackStack() before you commit the transaction: 为此,请在提交事务之前调用addToBackStack():

// Works with either the framework FragmentManager or the
// support package FragmentManager (getSupportFragmentManager).
getSupportFragmentManager().beginTransaction()
                       .add(detailFragment, "detail")
                       // Add this transaction to the back stack
                       .addToBackStack()
                       .commit();

Adding to backstack will solve the problem: 添加到backstack将解决问题:

periodFragmentTransaction.replace(R.id.frame, periodFragment);
periodFragmentTransaction.addToBackStack(null);
periodFragmentTransaction.commit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM