Previous Fragment出现在当前片段的方向更改上

时间:2014-09-24 12:44:34

标签: android android-fragments

我有两个片段。在第一个片段中有一个按钮,在点击时将移动到第二个片段。当屏幕在第二个片段中旋转时,在方向改变后,我看到第一个片段而不是第二个片段。任何人都可以帮我解决这个问题吗?


代码如下: 活动代码:

    package com.andr.fragmentsorientationdemo;

    import android.os.Bundle;
    import android.app.Activity;
    import android.app.FragmentManager;
    import android.app.FragmentTransaction;
     import android.view.Menu;

       public class OrientationActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_orientation);
          FragmentManager fragmentManager = getFragmentManager();
          FragmentTransaction fragmentTransaction = 
          fragmentManager.beginTransaction();
          fragmentTransaction.replace(android.R.id.content, new Fragmentone()).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.orientation, menu);
        return true;
    }

    }

<br>Code for FragmentOne <br>
=============================
    package com.andr.fragmentsorientationdemo;

     import android.app.Fragment;
      import android.app.FragmentManager;
      import android.app.FragmentTransaction;
        import android.os.Bundle;
        import android.view.LayoutInflater;
             import android.view.View;
          import android.view.View.OnClickListener;
      import android.view.ViewGroup;
      import android.widget.Button;


        public class Fragmentone extends Fragment{
         @Override
       public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
             }

           @Override
               public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
                    View v = inflater.inflate(R.layout.fragment_one, container, false);
                 Button b = (Button)v.findViewById(R.id.button1);
                 b.setOnClickListener(new OnClickListener()
                       {

                        @Override
                        public void onClick(View arg0) {
                  FragmentManager fragmentManager = getFragmentManager();
                  FragmentTransaction fragmentTransaction = 
                  fragmentManager.beginTransaction();
                  fragmentTransaction.replace(android.R.id.content, new Fragmenttwo()).addToBackStack(null).commit();

            }

        });
        return v;
    }
        }


<br>code for Fragmenttwo
===================

                                                    package com.andr.fragmentsorientationdemo;

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

                                    /**
                                     * @author 245742
                                     *
                                     */
                                    public class Fragmenttwo extends Fragment{
                                    @Override
                                    public void onCreate(Bundle savedInstanceState) {
                                        // TODO Auto-generated method stub
                                        super.onCreate(savedInstanceState);
                                        setRetainInstance(true);
                                    }

                                    @Override
                                        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                                Bundle savedInstanceState) {
                                            View v = inflater.inflate(R.layout.fragment_two, container, false);
                                            Button b = (Button)v.findViewById(R.id.button1);
                                            return v;
                                        }
                                    }


<br>layout file for fragmentone <br>
=====================================
                    <?xml version="1.0" encoding="utf-8"?>
                    <LinearLayout       xmlns:android="http://schemas.android.com/apk/res/android"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical" >

                        <Button
                            android:id="@+id/button1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Button" />

                    </LinearLayout>


<br> Layout for fragmenttwo
==============================

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

                <Button
                    android:id="@+id/button1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button2222222222" />

            </LinearLayout>


任何人都可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

试试这个 -

FragmentManager fragmentManager =null;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_orientation);
          if(fragmentManager==null){
          fragmentManager = getFragmentManager();
          FragmentTransaction fragmentTransaction = 
          fragmentManager.beginTransaction();
          fragmentTransaction.replace(android.R.id.content, new Fragmentone()).commit();
        }

    }

在manifest的活动标签中使用它 -

android:configChanges="orientation"