addToBackStack退出应用程序

时间:2014-07-19 23:36:58

标签: android fragment

我在提交'replace'事务之前调用addToBackStack。我的理解是,按下android内置的“后退”按钮会导致事务反转,将焦点恢复到原始片段,但应用程序关闭。这不是崩溃;我的日志显示没有错误。我的想法是backstack是空的,这是在空的Backstack上点击'back'的默认行为,但是backstack 不应该为空。我对此很新,所以我想知道我是不是应该实现我应该拥有的东西。

public class MainActivity extends ActionBarActivity {

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

    String[] names = new String[] {"Stephen Fry", "Bertrand Russell", "Hugh Laurie", "Angela Merkel"};
    final String[] descriptions = new String[] {"Comedian, actor, quizmaster, and all around goodie",
                                            "Mathematician, philosopher, maker of sacreligious kettles",
                                            "Comedian, pianist, and fake TV doctor",
                                            "German chancellor, EU ringleader, and bad haircut haver"};


    final FragmentManager fm = getFragmentManager();

    final TextFragment descriptionsFragment = (TextFragment) fm.findFragmentById(R.id.descriptions_fragment);
    final TextView descriptionsView = (TextView) findViewById(R.id.descriptions_view);

    ListAdapter namesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names);

    final ListFragment namesFragment = new ListFragment(){
        @Override
        public void onListItemClick(ListView l, View v, int position, long id){

            fm.beginTransaction().add(R.id.container, descriptionsFragment).addToBackStack(null).commit();
            descriptionsView.setText(descriptions[position]);

        }
    };
    namesFragment.setListAdapter(namesAdapter);

    fm.beginTransaction().replace(R.id.container, namesFragment).commit();




}

活动布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.twofragmentsthree.MainActivity"
tools:ignore="MergeRootFrame" >

<fragment android:name="com.example.twofragmentsthree.TextFragment"
    android:id="@+id/descriptions_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

        <TextView
        android:id="@+id/descriptions_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />



</fragment>

0 个答案:

没有答案