I have a tabbed interface in Android and use a FrameLayout as my main layout in my app. I'm getting an Android Lint warning that says:
This <FrameLayout> can be replaced with a <merge\> tag
The only place I use this FrameLayout (named fragmentContainer) is in the onTabSelected listener. Here's a simplified version.
@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, show the tab contents in the
// container
Fragment fragment;
String tag;
switch (tab.getPosition()) {
case 0:
fragment = new myFragment1();
tag = "myFragment1";
break;
case 1:
fragment = new new myFragment2();
tag = "myFragment2";
break;
default:
Log.d(TAG, "invalid tab selected: " + tab.getPosition());
break;
}
fragmentTransaction.replace(R.id.fragmentContainer, fragment, tag);
}
This works well, but I'd love to improve performance if possible, and get rid of any Lint warnings I can. If I replace the FrameLayout with a merge tag, I can't call that fragmentTransaction() because the id doesn't exist anywhere.
Here's the FrameLayout for completeness:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />
If that is the only thing in your activity's layout file, get rid of the layout file and your setContentView()
call, and use android.R.id.content
as the target of your FragmentTransaction
. android.R.id.content
points to the FrameLayout
that is where setContentView()
inflates into.
Also, please note that action bar tabs are deprecated in the "L" Developer Preview and should remain deprecated in the next production release of Android. You may wish to consider using another tab solution (e.g., ViewPager
and a tabbed indicator) or another navigation solution (e.g., a navigation drawer).