Android Fragment Back press

时间:2016-11-04 13:06:31

标签: java android android-fragments

我有几个片段。但是当按下按钮时,所有碎片都会导致PeriodFragment。我的问题是如何实现,以便当用户按下手机上的BACK按钮时,PeriodFragment将切换回输入的片段。 这是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;
    }

}

这是我的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;
    }

}

我是android studio的新手,所以请详细说明。

2 个答案:

答案 0 :(得分:1)

According to android docs在应用中使用片段时,各个FragmentTransaction对象可能表示应添加到后台堆栈的上下文更改。例如,如果您通过交换片段在手机上实现主/详细信息流,则应确保按详细信息屏幕上的“返回”按钮可将用户返回到主屏幕。为此,请在提交事务之前调用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();

答案 1 :(得分:0)

添加到backstack将解决问题:

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