6

Android 4.1ActionBarは、リストまたはタブとして便利なナビゲーションモードを提供します。SpinnerAdapterビューに表示する3つのフラグメントから選択するためにを使用していますandroid.R.id.content。次に、onNavigationItemSelected()リスナーは各フラグメントをビューにインフレーションし、を使用してバックスタックに追加しますFragmentTransaction.addToBackStack(null)

ActionBarこれはすべて宣伝どおりに機能しますが、現在のバックスタックを反映するようにを更新する方法がわかりません。作品を使用するActionBar.setSelectedNavigationItem(position)だけでなく、新しいものをトリガーして、OnNavigationListener()別のものを作成しますFragmentTransaction(私が望む効果ではありません)。明確にするために、コードを以下に示します。解決策についての助けをいただければ幸いです。


public class CalcActivity extends Activity {
private String[] mTag = {"calc", "timer", "usage"};
private ActionBar actionBar;

/** An array of strings to populate dropdown list */
String[] actions = new String[] {
    "Calculator",
    "Timer",
    "Usage"
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.actionBar = getActionBar();

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    // may not have room for Title in actionbar
    actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

    actionBar.setListNavigationCallbacks(
    // Specify a SpinnerAdapter to populate the dropdown list.
    new ArrayAdapter<String>(
        actionBar.getThemedContext(),
        android.R.layout.simple_list_item_1,
        android.R.id.text1,
        actions),
    // Provide a listener to be called when an item is selected.
    new NavigationListener()
    );      
}

public class NavigationListener implements ActionBar.OnNavigationListener {
    private Fragment mFragment;
    private int firstTime = 0;

    public boolean onNavigationItemSelected(int itemPos, long itemId) {
        mFragment = getFragmentManager().findFragmentByTag(mTag[itemPos]);

        if (mFragment == null) {
            switch (itemPos) {
            case 0:
                mFragment = new CalcFragment();
                break;
            case 1:
                mFragment = new TimerFragment();
                break;
            case 2:
                mFragment = new UsageFragment();
                break;
            default:
                return false;
            }               
            mFragment.setRetainInstance(true);
        }

        FragmentTransaction ft = getFragmentManager().beginTransaction();
        if (firstTime == 0) {
            firstTime++;
            ft.add(android.R.id.content, mFragment, mTag[itemPos]);
        } else {
            ft.replace(android.R.id.content, mFragment, mTag[itemPos]);
            ft.addToBackStack(null);
        }
        ft.commit();

        Toast.makeText(getBaseContext(), "You selected : " + 
                actions[itemPos], Toast.LENGTH_SHORT).show();

        return true;
    }       
}

public static class CalcFragment extends Fragment {             
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_calc, container, false);
    return v;
    }   
}

public static class TimerFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_timer, container, false);
    return v;
    }   
}

public static class UsageFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_usage, container, false);
    return v;
    }
}
4

1 に答える 1

4

あなたはこのようなことをすることができます:

  1. 戻るボタンに基づいてナビゲーションアイテムを選択するときに追跡するブール値を作成します。

    private boolean mListIsNavigatingBack = false;
    
  2. onBackPressedをオーバーライドします。オーバーライドで、バックスタックにアイテムがあるかどうかを確認します。アイテムがある場合は、自分で処理します。ない場合は、スーパークラスを呼び出します。

    public void onBackPressed() {
        if(getFragmentManager().getBackStackEntryCount() > 0){
            mListIsNavigatingBack = true;
    
            //You need to get the previous index in the backstack through some means
            //possibly by storing it in a stack
            int previousNavigationItem = ???;
    
            getActionBar().setSelectedNavigationItem(previousNavigationItem);
        }
        else{
            super.onBackPressed();
        }
    }
    
  3. NavigationListener内で、mListIsNavigatingBack状態を処理し、手動でバックスタックをポップして、状態の設定を解除します。

    if(mListIsNavigatingBack){
        if(fm.getBackStackEntryCount() > 0){
            fm.popBackStack();
        }
        mListIsNavigatingBack = false;
    }
    
于 2012-10-17T23:13:48.330 に答える