我怎么能用android Fragment backstack?

时间:2012-12-19 11:44:25

标签: android android-fragments

我有四个片段A B C D.当我从A跳到B和从B跳到C和从C跳到D.当我按回键时,如何将D的结果变为B?

1 个答案:

答案 0 :(得分:1)

<强> FragmentD:

在FragmentD中创建一个接口。

OnBackListener mback;
    public interface OnBackListener {
        public void onBackClicked();
}

@Override
    public void onAttach(Activity activity) {
        try {
            mback = (OnBackListener ) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnBackListener");
        }
        super.onAttach(activity);
    }


public void methodCall()
    {
      mback.onBackClicked()
    }

然后

家长活动

实现我们在FragmentD中创建的OnBackListener接口。

 public class MyActivity extends FragmentActivity implements OnBackListener {


// overriding OnBackListener interface method
        @Override
            public void onBackClicked() {

                  //calling fragmentb
            }   

    //Backkey event
        @Override
        public void onBackPressed()
        {
            super.onBackPressed();
            fragmentd.methodCall();
        }

        }

顺便说一句,你会得到fragmentD到fragmentB的结果。