【问题标题】:Fragment activity gets destroyed when entering new activity from fragment and going back从片段进入新活动并返回时,片段活动被破坏
【发布时间】:2013-10-01 21:04:01
【问题描述】:

我有一个 FragmentActivity 类型的活动。在里面我有一些使用视图寻呼机的内容,其中每个可滚动选项卡都是一个片段:

<android.support.v4.view.ViewPager      
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".OpenStack" >

    <!--
    This title strip will display the currently visible page title, as well as the page
    titles for adjacent pages.
    -->

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="#fff" />

</android.support.v4.view.ViewPager>

在片段中,当用户点击列表中的项目时,我正在打开一个新活动,如下所示:

Intent myIntent = new Intent(getActivity(), InstanceActivity.class);
getActivity().startActivity(myIntent);

但是当我在 InstaceActivity 中单击返回以返回到带有片段的那个时,它会被销毁并再次创建。我不知道为什么会这样。我应该做一些特别的事情来将 FragmentActivity 保留在后台堆栈中吗?

它甚至没有帮助保存状态:

public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("CONNTOKEN", isConnToken);
    outState.putString("NOVAURL", isNovaURL);
}   

因为在 FragmentActivity 的 oncreate 事件中,当我按下返回时,savedInstanceState 为 NULL ?

【问题讨论】:

    标签: android android-fragmentactivity back-stack


    【解决方案1】:

    试试这个:addToBackStack。 ;)

    【讨论】:

    • 据我了解,这是从片段到另一个片段时使用的,但这里我是从片段到活动。
    • 我这里不改fragment,只能在FragmentTransaction上使用addToBackStack。无论如何我都试过了,但它没有改变任何东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多