2016-05-08 4 views
-1

2つの設定があり、縦向きにロックされたフラグメントが表示されます。Android:オリエンテーションの変更でクラス変数がクリアされないようにするには

このフラグメントでボタンをクリックすると、フラグメントが置換されますが、この新しいフラグメントは、横に並べて2番目のフラグメントとともに表示されます。

これはすべてうまくいきますが、2番目の状態でバックナビゲーションを傍受して、元の設定に置き換えることができます。向きを変えると、すべてのクラス変数がリセットされ、フラグメントへの参照が失われます。ここで

主な活動です:

パッケージmobileapp.group2.fragmenttest。

import android.content.pm.ActivityInfo; 
import android.content.res.Configuration; 
import android.support.v4.app.FragmentActivity; 
import android.os.Bundle; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.View; 
import android.widget.FrameLayout; 
import android.widget.Toast; 

public class MainActivity extends FragmentActivity 
    implements ListPeersFragment.ListPeersFragmentListener 
{ 
    public static final String LIST_FRAGMENT_TAG = "ListPeersFragment"; 
    public static final String GAME_FRAGMENT_TAG = "GameFragment"; 
    public static final String OPP_FRAGMENT_TAG = "ViewOpponentFragment"; 

    // FrameLayout references 
    private FrameLayout mLeftFrame; 
    private FrameLayout mRightFrame; 

    // Fragment references 
    private ListPeersFragment mListPeersFragment = null; 
    private GameFragment  mGameFragment  = null; 
    private ViewOppFragment mViewOppFragment = null; 

    // Boolean denoting if currently in game mode. 
    //private boolean mGameMode; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mLeftFrame = (FrameLayout) findViewById(R.id.left_frame); 
     mRightFrame = (FrameLayout) findViewById(R.id.right_frame); 

     // However, if we're being restored from a previous state, 
     // then we don't need to do anything and should return or else 
     // we could end up with overlapping fragments. 
     if (savedInstanceState != null) { 
      return; 
     } 

     // Create fragments 
     if (mListPeersFragment == null) 
      mListPeersFragment = new ListPeersFragment(); 
     if (mGameFragment == null) 
      mGameFragment = new GameFragment(); 
     if (mViewOppFragment == null) 
      mViewOppFragment = new ViewOppFragment(); 

     // In case this activity was started with special instructions from an 
     // Intent, pass the Intent's extras to the fragment as arguments 
     mListPeersFragment.setArguments(getIntent().getExtras()); 

     // Add the fragment to the 'fragment_container' FrameLayout 
     FragmentManager fm = getSupportFragmentManager(); 
     fm.beginTransaction().add(R.id.left_frame, mListPeersFragment, LIST_FRAGMENT_TAG).commit(); 
     fm.beginTransaction().add(R.id.right_frame, mViewOppFragment, OPP_FRAGMENT_TAG).commit(); 

     //mGameMode = false; 
    } 

    // Used to intercept back navigation. 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_BACK) { 
      FragmentManager fm = getSupportFragmentManager(); 
      if (fm.findFragmentByTag(GAME_FRAGMENT_TAG) != null) 
      { 
       setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
       //FragmentTransaction ft = fm.beginTransaction(); 
       fm.beginTransaction().replace(R.id.left_frame, mListPeersFragment, LIST_FRAGMENT_TAG) 
         .commitAllowingStateLoss(); 
       //mGameMode = false; 
       return true; 
      } 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

    public void startGame() 
    { 
     getSupportFragmentManager().beginTransaction() 
       .replace(R.id.left_frame, mGameFragment, GAME_FRAGMENT_TAG).commit(); 

     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 
     //mGameMode = true; 
    } 

    // Implementation of the ListPeersFragmentListener function onPeerSelected 
    public void onPeerSelected(int position) 
    { 

    } 

} 

メインアクティビティのレイアウトファイル:

<?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="horizontal" > 

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

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

</LinearLayout> 

風景のバージョンはちょうどframelayouts上の重みを持っています。

他のフラグメントクラスは非常に単純で、現在のところ何もしません。

失敗したコードはonKeyDownメソッドです。クラス変数が方向変更に再初期化されないようにする方法はありますか?私は設定の変更を処理することを見てきましたが、ランドスケープレイアウトファイルはロードされません。

誰かが助けてくれれば非常に感謝します!

+0

方向変更時にフラグメントの再作成を停止するには、 'setRetainInstance(true)'を使用できます。 http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean) –

+0

サービスMVCデザインを開始しました – Pomagranite

答えて

0

実際にオリエンテーションが変更されると、アクティビティのOncreate()メソッドが呼び出されます。だから、変数のクリアを防ぐOnConfigurationChange()メソッドをオーバーライドして、マニフェストファイル内での活動でこれを行うには: -

android:configChanges="orientation" 
0

this linkで述べたようにあなたがonSaveInstanceState()とアクティビティのonRestoreInstanceState()メソッドを使用する必要があります。

また、保存するフラグメントのsetRetainInstance()メソッドを呼び出す必要があります。

setRetainInstanceState()についての詳細は、this linkをご覧ください。

関連する問題