为什么轮换导致Android片段替换失败?

时间:2015-06-05 16:20:48

标签: java android android-fragments

我已经整理了一个简单的程序,它使用单个活动和两个片段替换片段。一个片段有一个按钮,按下时,用第二个片段替换片段。

如果启动应用程序并单击按钮,则按预期工作。 First Fragment Fragment replacement success

如果应用程序启动,设备将旋转(横向或继续回到纵向),单击按钮,然后片段替换失败,同时显示两个片段。 Fragment replacement failure

这里发生了什么?我没有解决片段生命周期问题吗?

MainActivity.java

package edu.mindlab.fragmenttest;

import android.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends ActionBarActivity{
    private MainActivityFragment startingFragment;
    private ReplacementFragment replacementFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startingFragment = new MainActivityFragment();
        replacementFragment = new ReplacementFragment();

        FragmentTransaction t = getFragmentManager().beginTransaction();
        t.add(R.id.fragment_container, startingFragment, "start").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);
    }

    public void replaceFragment(View view){
        if(!replacementFragment.isAdded()) {
            FragmentTransaction t = getFragmentManager().beginTransaction();
            t.replace(R.id.fragment_container, replacementFragment, "replacementTest").commit();
        }
    }
}

MainActivityFragment.java

package edu.mindlab.fragmenttest;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainActivityFragment extends Fragment {
    public MainActivityFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);

        return view;
    }
}

ReplacementFragment.java

package edu.mindlab.fragmenttest;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ReplacementFragment extends Fragment {

    public static ReplacementFragment newInstance() {
        ReplacementFragment fragment = new ReplacementFragment();
        return fragment;
    }

    public ReplacementFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_replacement, container, false);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

}

activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:id="@+id/fragment_container">
</FrameLayout>

fragment_main.xml

<RelativeLayout 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: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=".MainActivityFragment">

<TextView
    android:text="@string/start_string"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/replacement_fragment"
    android:id="@+id/button"
    android:layout_below="@+id/textView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="56dp"
    android:onClick="replaceFragment"/>


</RelativeLayout>

fragment_replacement.xml

<FrameLayout 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"
         tools:context="edu.mindlab.fragmenttest.ReplacementFragment">

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/replacement_fragment"/>

</FrameLayout>

2 个答案:

答案 0 :(得分:4)

答案很简单。屏幕旋转时保留UI状态,这意味着如果您不执行任何操作,屏幕在旋转后仍会显示第二个片段。由于您在Activity onCreate方法中添加了第一个片段,因此它将被添加到已经显示的内容中,因此两个片段重叠。

你可以这样做:

FragmentManager mgr = getFragmentManager();
if (mgr.findFragmentByTag("start") == null &&
    mgr.findFragmentByTag("replacementTest") == null) {

    FragmentTransaction t = mgr.beginTransaction();
    t.add(R.id.fragment_container, startingFragment, "start").commit();             

}

答案 1 :(得分:3)

@Emanuel是正确的,UI状态在屏幕旋转中保留。然而,正在发生的事情只是您的Activity的onCreate总是在轮换时被调用,它总是会添加startingFragment

解决方案很简单。只需在onCreate中添加一个条件就可以处理它作为活动生命周期的一部分被调用的情况:

if (savedInstanceState == null)
        {
            FragmentTransaction t = getFragmentManager().beginTransaction();
            t.add(R.id.fragment_container, startingFragment, "start").commit();
        }

你可能想考虑做

t.replace(R.id.fragment_container, startingFragment, "start").commit();

但不是

t.add(R.id.fragment_container, startingFragment, "start").commit();