addToBackStack()不起作用。回到家不是以前的片段

时间:2014-09-23 06:52:27

标签: android android-fragments

我对android很新。当我尝试使用addToBackStack()时,遇到了问题。

当我按下片段2中的后退按钮时,它不会转到片段1而是回到家。

为什么呢?我的代码有什么问题吗? 谢谢!

MaiinActivity.java

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager mFragmentManager = getFragmentManager();
        FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction();

        mFragmentTransaction.add(R.id.id_frame, new firstFrag(), "firstFrag");
        mFragmentTransaction.commit();


    }
}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.myfragtest2.MainActivity" >

    <FrameLayout 
        android:id="@+id/id_frame"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"

        ></FrameLayout>

</RelativeLayout>

firstFrag.java

public class firstFrag extends Fragment {


    private Button button1;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

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

        button1 = (Button)view.findViewById(R.id.btn1);
        button1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                FragmentManager myFragmentManager = getFragmentManager();
                FragmentTransaction myFTransaction = myFragmentManager.beginTransaction();
                myFTransaction.replace(R.id.id_frame, new secondFrag());
                myFTransaction.addToBackStack(null);
                myFTransaction.commit();



            }
        });

        return view;
    }


}

first_frag.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText 
        android:id="@+id/et1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="this is frag 1!!"
        />

    <Button 
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="move to frag 2"
        />


</LinearLayout>

3 个答案:

答案 0 :(得分:3)

在主要活动中添加此代码段

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() == 0) {
        super.onBackPressed();
    } else {
        getFragmentManager().popBackStack();
    }
}

记录碎片的数量。

答案 1 :(得分:0)

而不是null使用addToBackStack()中的标记名称,而不是oncall。 我使用了以下代码,它为我工作:

FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.add(R.id.mainfragment, new Fragment1(), "Fragment1");
ft.addToBackStack("Fragment1");             
ft.commit();

答案 2 :(得分:0)

如前所述,您应该在提交之前使用addToBackStack(String name)方法。但真正的问题是您的MainActivity扩展了ActionBarActivity。看起来它会覆盖Back Button行为。尝试扩展活动。应该工作正常。在这种情况下,你必须处理可能的Action Bar不当行为。

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getFragmentManager().beginTransaction()
            .add(R.id.container_main, new ListFragment())
            .commit();
}

public void startDetailFragment (View view) {
    getFragmentManager().beginTransaction()
            .replace(R.id.container_main, new DetailFragment())
            .addToBackStack(null)
            .commit();
}