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" />
Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container.
Android Essentials: <merge> tag The merge tag does exactly that – it merges its contents into the parent layout. This allows us to avoid duplicated layouts and flattens the view hierarchy. Occasionally you will find the need to create a custom view by extending an ViewGroup.
Android FragmentTransaction It gives us an interface for interacting with fragments. fragmentTransaction.replace(R.id.fragment_container, mFeedFragment); The method replace(int containerViewId, Fragment fragment) replaces an existing Fragment object from the container containerViewId and adds the the Fragment fragment.
Merge tags are also known as personalization fields or personalization tags. Merge tags allow you to insert ("merge") data from your mailing list directly into your email campaigns. For example, if you'd like to insert your subscriber's first name into your email, you use the merge tag #[FNAME]#.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With