屏幕旋转时,findFragmentById(id)未正确返回

时间:2014-01-04 03:52:59

标签: android android-fragments screen-rotation

当我尝试在屏幕旋转上使用片段时,我遇到了问题

我的代码来自android training with Fragment上的FragmentBasics,但我改为使用layout-land布局大 -

RES /布局脊/ activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false" >

<fragment
    android:id="@+id/headlines_fragment"
    android:name="com.example.fragmentoverhoneycomb.HeadlinesFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />

<fragment
    android:id="@+id/article_fragment"
    android:name="com.example.fragmentoverhoneycomb.Articles"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2" />

MainActivity

public class MainActivity extends Activity 
implements OnHeadlineSelectedListener {

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

    if(findViewById(R.id.fragment_container) != null) {
        if (savedInstanceState != null) {
            return;
        }

        HeadlinesFragment firstFragment = new HeadlinesFragment();

        firstFragment.setArguments(getIntent().getExtras());

        getFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onArticleSelected(int position) {
    // TODO Auto-generated method stub
    Articles articleFrag = (Articles) getFragmentManager().findFragmentById(R.id.article_fragment);
    if (articleFrag != null) {
        articleFrag.updateArticleView(position);
    } else {
        Articles newFragment = new Articles();
        Bundle args = new Bundle();
        args.putInt(Articles.ARG_POSITION, position);
        newFragment.setArguments(args);
        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);

        transaction.commit();
    }
}

Articles.java

public class Articles extends Fragment {
final static String ARG_POSITION = "position";
int mCurrentPosition = -1;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (savedInstanceState != null) {
        mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
    }

    return inflater.inflate(R.layout.activity_article, container, false);
}

@Override
public void onDestroyView() {
    super.onDestroyView();
}

@Override
public void onStart() {
    super.onStart();
    Bundle args = getArguments();
    if(args != null) {
        updateArticleView(args.getInt(ARG_POSITION));
    } else if (mCurrentPosition != -1) {
        updateArticleView(mCurrentPosition);
    }
}

public void updateArticleView(int position) {
    TextView article = (TextView) getActivity().findViewById(R.id.textViewArticle);
    article.setText(Ipsum.Articles[position]);
    mCurrentPosition = position;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putInt(ARG_POSITION, mCurrentPosition);
}

}

所以我的想法是在肖像时使用单片段并在景观时组合两个片段, 但是当我这样做时我收到错误:

Step.1以肖像状态开始并通过onArticleSelected显示article_fragment一次显示文章,然后返回HeadlinesFragment

Step.2 Ctrl-F11两次(肖像 - &gt;横向 - &gt;肖像)

Step.3重做Step.1然后发生错误:

Articles articleFrag = (Articles) getFragmentManager().findFragmentById(R.id.article_fragment);

返回不为空 当我调试它时,我发现onCreateView()函数在Step.3发生者时没有调用

1 个答案:

答案 0 :(得分:0)

您在代码中创建了片段,这意味着您无法为其提供任何ID,因此无法找到该片段。您可以使用片段的标记来识别它,方法是使用重载的add方法为其标记:

getFragmentManager().beginTransaction()
    .add(R.id.fragment_container, firstFragment, "fragmenttag").commit();

然后,您可以通过此标记找到您的片段:

Articles articleFrag = (Articles) getFragmentManager().findFragmentByTag("fragmenttag");

最好使片段标签保持不变。