【问题标题】:findFragmentById(id) didn't return correctly when screen rotate屏幕旋转时 findFragmentById(id) 没有正确返回
【发布时间】:2014-01-04 03:52:58
【问题描述】:

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

我的代码来自 android training with Fragment 上的 FragmentBasics,但我用 layout-land 代替了 layout-large

res/layout-land/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" />

主活动

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 两次(纵向->横向->纵向)

Step.3 重做Step.1 然后出现错误:

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

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

【问题讨论】:

    标签: android android-fragments screen-rotation


    【解决方案1】:

    您在代码中创建片段,这意味着您不能给它任何 id,因此无法找到片段。您可以使用片段的标签来识别它,通过使用重载的add方法给它一个标签:

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

    然后你可以通过这个标签找到你的片段:

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

    最好将片段标签设为常量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多