问题描述
- 隐藏和显示 Fragment 时显示错误的 fragment
- 第一次我把程序中的fragments的可见性改变了, fragment显示的不是我设置的。
My layout:<LinearLayout 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:orientation=""vertical"" tools:context="".MainActivity"" > <fragment android:id=""@+id/act_main_frag_top_bar"" class=""br.com.myapp.fragments.TopBarFragment"" android:layout_height=""wrap_content"" android:layout_width=""match_parent""/> <fragment android:id=""@+id/act_main_frag_toolbar"" class=""br.com.myapp.fragments.ToolbarFragment"" android:layout_height=""wrap_content"" android:layout_width=""match_parent""/> <!-- Tabs --> <fragment android:id=""@+id/act_main_frag_twitter"" class=""br.com.myapp.fragments.TwitterFragment"" android:layout_height=""match_parent"" android:layout_width=""match_parent"" android:visibility=""gone""/> <fragment android:id=""@+id/act_main_frag_facebook"" class=""br.com.myapp.fragments.FacebookFragment"" android:layout_height=""match_parent"" android:layout_width=""match_parent"" android:visibility=""gone""/> <fragment android:id=""@+id/act_main_frag_tv_show"" class=""br.com.myapp.fragments.TVShowFragment"" android:layout_height=""match_parent"" android:layout_width=""match_parent"" android:visibility=""gone""/> <fragment android:id=""@+id/act_main_frag_info"" class=""br.com.bluepen.mixtv.fragments.InfoFragment"" android:layout_height=""match_parent"" android:layout_width=""match_parent"" android:visibility=""gone""/> </LinearLayout>
myActivity:
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager manager = getSupportFragmentManager(); mStreamingFrag = (StreamingFragment) manager.findFragmentById(R.id.act_main_frag_streaming); mFacebookFrag = (FacebookFragment) manager.findFragmentById(R.id.act_main_frag_facebook); mTwitterFrag = (TwitterFragment) manager.findFragmentById(R.id.act_main_frag_twitter); mTVShowFrag = (TVShowFragment) manager.findFragmentById(R.id.act_main_frag_tv_show); mInfoFrag = (InfoFragment) manager.findFragmentById(R.id.act_main_frag_info); mToolbar = (ToolbarFragment) manager.findFragmentById(R.id.act_main_frag_toolbar); mToolbar.setListener(this); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.show(mStreamingFrag); transaction.commit();}
在onCreate方法执行后,fragment显示的是 TwitterFragment,而不是我在onCreate方法的最后设置的StreamingFragment 。
时间: 2024-10-21 11:04:05