wbk727
wbk727

Reputation: 8408

ListFragment appearing twice on phone

I'm seeing an undesired appearance of my list fragment when I run my app on the phone emulator. On the tablet emulator it appears once but on the phone emulator it appears twice. How can I fix this error for the list to appear on the phone emulator only once?

MainActivity.java

public class MainActivity extends ActionBarActivity {

    private boolean mTwoPane;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentMainList newFragment = new FragmentMainList();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.master_container, newFragment);
        transaction.commit();

        if (findViewById(R.id.detail_container) != null) {
            mTwoPane = true;
        }
    }
}

activity_main.xml

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/master_container"
    android:name="com.apptacularapps.exitsexpertlondonlite.FragmentMainList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FragmentMainList"
    tools:layout="@android:layout/list_content"/>

activity_main.xml (sw600dp)

<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="horizontal"
    android:showDividers="middle"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/master_container"/>

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:id="@+id/detail_container"/>

</LinearLayout>

FragmentMainList.java

public class FragmentMainList extends android.support.v4.app.Fragment {

    ListView list_main;

    String[] listContent = {
            "Item 1",
            "Item 2",
            "Item 3"
    };

    private boolean mTwoPane;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.fragment_main_list, container, false);
        list_main = (ListView)v.findViewById(R.id.list_main);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,listContent);
        list_main.setAdapter(adapter);

        return v;
    }
}

Upvotes: 4

Views: 124

Answers (2)

Vasiliy
Vasiliy

Reputation: 16238

Although you've already understood how to make it work, I'll try to explain how to make it work properly.

What you do is in fact this:

  • If on small screen - add the fragment statically (in XML)
  • If on larger screen - add the fragment dynamically (with FragmentTransaction)

It looks weird that the method by which you get the desired functionality depend on the screen size - there should be no correlation between these factors.

What you ought to do (IMHO) is to add this fragment dynamically in both cases - just change your Fragment tag in the first XML to FrameLayout and everything will work as expected.

Upvotes: 1

km86
km86

Reputation: 485

Please move the logic to add fragment inside the if loop you have.

Upvotes: 2

Related Questions