Replacing a fragment does not replace the previous fragment entirely. Why so?

时间秒杀一切 提交于 2019-12-02 01:50:48

问题


I'm implementing a "fragments-101" program, where-in I replace a fragment when its corresponding button is clicked. However, the below thing happens:

Why does this happen? Why is the initial fragment not replaced completely? MainActivity and both fragment xml files use LinearLayout. I searched here about the problem, and found that the replace() method can sometimes be buggy, so I tried using add(), but to no avail. There is no special code in the fragments, it's a simple hello-world-type fragment program.

I'm using Android Lollipop version in the AVD as well as the targetSDK, with minSDK version corresponding to Android 4.0.3, in Android Studio 1.0.2. Can this be something related to Android Lollipop?

MainActivity.java

package com.example.fragmenttest;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends Activity {

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

    }

    public void launchFragment(View v)
    {
        Fragment frag;

        if(v == findViewById(R.id.btnFrag2))
        {
            frag = new FragmentTwo();
        }
        else
        {
            frag = new FragmentOne();
        }

        fm = this.getFragmentManager();
        ft = fm.beginTransaction();
        ft.replace(R.id.myFrag,frag);
        ft.commit();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

layout_frag1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ffff">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment 1."
        android:id="@+id/textView2" />
</LinearLayout>

layout_frag2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffff00">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment 2."
        android:id="@+id/textView3" />
</LinearLayout>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button
        android:layout_width="348dp"
        android:layout_height="wrap_content"
        android:text="Launch Fragment 2"
        android:id="@+id/btnFrag2"
        android:onClick="launchFragment"
        android:layout_gravity="center_vertical" />

    <Button
        android:layout_width="355dp"
        android:layout_height="wrap_content"
        android:text="Launch Fragment 1"
        android:id="@+id/btnFrag1"
        android:onClick="launchFragment"/>

    <fragment
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:name="com.example.fragmenttest.FragmentTwo"
        android:id="@+id/myFrag"
        tools:layout="@layout/layout_frag2" />

</LinearLayout>

回答1:


Fragments that are inflated directly from xml layout with <fragment/> tag cannot be replaced.

You should have an empty FrameLayout inside your xml layout. And you should add, remove, replace all your Fragments programmatically into that FrameLayout.



来源:https://stackoverflow.com/questions/27632443/replacing-a-fragment-does-not-replace-the-previous-fragment-entirely-why-so

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!