2014-09-24 44 views
0

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


的代码如下: 活动代码:

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> 


谁能帮我解决这个问题。

回答

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(); 
     } 

    } 

否则

使用该清单中的活动标签 - 的活动标签

android:configChanges="orientation" 
+0

Manifest条目为我工作。多谢,伙计 – 2014-09-24 17:19:27