2014-01-10 78 views
0

我的Android應用程序出現非常令人不安的麻煩。我正在使用Fragments。每個片段都包含一個用於Portrait UI和Landscape UI的獨立XML文件。請勿在旋轉時加載相關的橫向/縱向佈局

如果我從片段'InputFragment'移動到片段'OutputFragment',並且如果我現在旋轉設備,則片段變爲'InputFragment'!這意味着,應用程序正在旋轉時返回到前一個片段!但是,這不是應該發生的。如果我將設備旋轉到縱向位置,那麼它應該調用相同片段的橫向UI,反之亦然。

以下是驗證碼。我只顯示代碼的結構,因爲所有其它的變量聲明和分配值等

InputFragment.java

public class InputFragment extends Fragment { 

    //Declaring variables. CODE REMOVED 




    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     //Intializing instance variables. CODE REMOVED 

     view = inflater.inflate(R.layout.input, container,false); 



     restoreStates(savedInstanceState); 

     return view; 

    } 

    //This method will save the instances 
    @Override 
    public void onSaveInstanceState(Bundle savedInstanceStateBundle) 
    { 
     super.onSaveInstanceState(savedInstanceStateBundle); 

     try{ 

     } 
     catch(NullPointerException e) 
     { 

     } 
    } 

    //This method will restore the instances 
    @Override 
    public void onActivityCreated(Bundle savedInstanceStateBundle) 
    { 
     super.onActivityCreated(savedInstanceStateBundle); 

     if(savedInstanceStateBundle!=null) 
     { 
     } 
    } 

    //Action Listener for the Submit Bar 
    private class SubmitButtonAction implements OnClickListener 
    { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      try { 
       //Put the JSON. Calling to the JSON handling class. CODE REMOVED 


      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      Fragment frag = new OutputFragment(); 

      FragmentTransaction ft = getFragmentManager().beginTransaction(); 
      ft.replace(R.id.fragment_container, frag); 
      ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
      ft.addToBackStack(null); 
      ft.commit(); 



     } 

    } 



    private void restoreStates(Bundle savedInstanceStateBundle) 
    { 
     if(savedInstanceStateBundle!=null) 
     { 

     } 
    } 




} 

OutputFragment.Java

public class OutputFragment extends Fragment implements AsyncResponse{ 

    //Delclaring variables. CODE REMOVED 


    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     //Intializing instance variables. CODE REMOVED 
     view = inflater.inflate(R.layout.output, container,false); 


     return view; 

    } 


    //ActionListener for the manual back button 
    private class BackButtonAction implements OnClickListener 
    { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      FragmentManager fm = getFragmentManager(); 
      fm.popBackStackImmediate(); 

     } 

    } 


    @Override 
    public void processFinish(String output) { 
     // TODO Auto-generated method stub 
     Log.d("OUTPUT_FRAGMENT", output); 

     try { 





      progressDialog.dismiss(); 

     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

} 

MainActivity.java

import android.os.Bundle; 
    import android.app.Activity; 
    import android.content.Intent; 
    import android.support.v4.app.Fragment; 
    import android.support.v4.app.FragmentActivity; 
    import android.support.v4.app.FragmentTransaction; 
    import android.view.Menu; 
    import android.view.Window; 

    public class MainActivity extends FragmentActivity { 

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


      Fragment frag = new InputFragment(); 

      FragmentTransaction ft = getSupportFragmentManager().beginTransaction();  
      ft.replace(R.id.fragment_container, frag); 
      ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
      ft.addToBackStack(null); 
      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.main, menu); 
      return true; 
     } 



} 

清單文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.xxx.xxx" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-permission android:name="android.permission.INTERNET" /> 

    <uses-sdk 
     android:minSdkVersion="11" 
     android:targetSdkVersion="18" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/xxx_logo" 
     android:label="@string/xxx" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name="com.xxx.xxx.MainActivity" 
      android:configChanges="keyboardHidden|screenLayout|orientation|screenSize" 
      android:label="@string/xxx" > 

     </activity> 
     <activity 
      android:name="com.xxx.xxx.SplashScreen" 
      android:configChanges="keyboardHidden|screenLayout|orientation|screenSize" 
      android:label="@string/xxx" > 

      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

搜尋小時後,我發現了一個解決方案,以阻止這個問題。也就是說,將代碼android:configChanges="keyboardHidden|screenLayout|orientation|screenSize"添加到Manifest文件中。

現在,片段不會移動到旋轉中的上一個片段,但它不會加載其他佈局!這意味着,如果將手機從縱向旋轉到橫向,則不會加載「橫向」UI,反之亦然!它只是試圖調整自己的視圖,這是不好的,因爲圖像和一切都顯示不好,沒有加載其他相關的用戶界面。這裏發生了什麼?

+0

你的佈局d因爲你使用了'android:configChanges'屬性,所以不會改變,因爲它基本上是用來防止佈局在旋轉時重新創建的 – Hellboy

回答

1

旋轉後,您的活動被重新創建並調用其onCreate()方法。 你想要做的是檢查活動是否是第一次啓動,然後才能替換你的片段InputFragment。在活動第一個創造savedInstanceState爲空,所以你可以做這樣的事情:

if(savedInstanceState == null) { 
    // replace fragment 
} 

編輯:

這是你onCreate()的活動應該是什麼樣子:

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

     if(savedInstanceState == null) { 
      Fragment frag = new InputFragment(); 

      FragmentTransaction ft = getSupportFragmentManager().beginTransaction();  
      ft.replace(R.id.fragment_container, frag); 
      ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
      ft.addToBackStack(null); 
      ft.commit(); 
     } 
    } 

現在,方向改變活動不會取代你的片段,你不需要在清單中設置任何attrs

+0

在哪個片段中我應該放這個代碼? –

+0

不在片段中,在您的活動'onCreate'方法中。把FragmentTransaction代碼放在​​那裏。並記得刪除清單中的'configChanges'屬性 – Hellboy

+0

我已更新我的回答 – Hellboy

相關問題