Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Lint says I can replace a layout with a <merge> tag, but I need the id of the layout in a fragment transaction

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" />
like image 852
DruLang Avatar asked Oct 12 '14 22:10

DruLang


People also ask

How to replace fragment with another fragment in android?

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.

What is merge tag in Android XML?

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.

What is fragment transaction in android?

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.

What is the use of Merge tag?

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]#.


1 Answers

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).

like image 56
CommonsWare Avatar answered Sep 21 '22 14:09

CommonsWare