Example usage for android.app FragmentManager addOnBackStackChangedListener

List of usage examples for android.app FragmentManager addOnBackStackChangedListener

Introduction

In this page you can find the example usage for android.app FragmentManager addOnBackStackChangedListener.

Prototype

public abstract void addOnBackStackChangedListener(OnBackStackChangedListener listener);

Source Link

Document

Add a new listener for changes to the fragment back stack.

Usage

From source file:oly.netpowerctrl.main.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    FragmentUtils.applyActivityFlags(this);

    super.onCreate(savedInstanceState);

    assignContentView();/*from  w w  w  . j av  a 2 s . com*/

    FragmentManager fm = getFragmentManager();
    fm.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            if (firstFragment) {
                firstFragment = false;
                return;
            }
            getSupportActionBar().setDisplayHomeAsUpEnabled(getFragmentManager().getBackStackEntryCount() > 1);
            //                if (getFragmentManager().getBackStackEntryCount() <= 1) {
            //                    Toast.makeText(MainActivity.this, getString(R.string.press_back_to_exit), Toast.LENGTH_SHORT).show();
            //                }
        }
    });
}

From source file:com.android.tv.ui.TvOverlayManager.java

private void runAfterSideFragmentsAreClosed(final Runnable runnable) {
    final FragmentManager manager = mMainActivity.getFragmentManager();
    if (mSideFragmentManager.isSidePanelVisible()) {
        manager.addOnBackStackChangedListener(new OnBackStackChangedListener() {
            @Override/*w ww  . j  a  va2 s  .c  o m*/
            public void onBackStackChanged() {
                if (manager.getBackStackEntryCount() == 0) {
                    manager.removeOnBackStackChangedListener(this);
                    runnable.run();
                }
            }
        });
    } else {
        runnable.run();
    }
}

From source file:it.scoppelletti.mobilepower.app.FragmentLayoutController.java

/**
 * Ripristina lo stato dell&rsquo;istanza.
 * /*from www . j  av  a 2s  .  c  o  m*/
 * <P>L&rsquo;attivit&agrave; ripristina lo stato dell&rsquo;istanza
 * {@code FragmentLayoutController} all&rsquo;interno del proprio metodo
 * {@code onRestoreInstanceState}.</P>
 * 
 * @param savedInstanceState Stato dell&rsquo;istanza.
 * @param fragmentCollector  Collettore dei frammenti di dettaglio. 
 */
public void onRestoreInstanceState(Bundle savedInstanceState,
        FragmentLayoutController.FragmentCollector fragmentCollector) {
    int n, oldPanelCount, tnId;
    String tag;
    ActivitySupport activitySupport;
    FragmentSupport fragment;
    FragmentManager fragmentMgr;
    FragmentLayoutController.BackStackChangedListener backStackListener;
    Queue<FragmentSupport> fragmentQueue;
    Queue<FragmentLayoutController.FragmentEntry> clonedQueue;

    if (savedInstanceState == null) {
        throw new NullPointerException("Argument savedInstanceState is null.");
    }
    if (fragmentCollector == null) {
        throw new NullPointerException("Argument fragmentCollector is null.");
    }

    if (!(myActivity instanceof ActivitySupport)) {
        myLogger.warn("Activity not implement interface ActivitySupport.");
        return;
    }

    oldPanelCount = savedInstanceState.getInt(FragmentLayoutController.STATE_PANELCOUNT, 0);
    if (oldPanelCount < 1) {
        myLogger.warn("Unexpected {}={} in saved instance state.", FragmentLayoutController.STATE_PANELCOUNT,
                oldPanelCount);
        return;
    }

    myLogger.debug("{}: current={}, saved instance state={}.",
            new Object[] { FragmentLayoutController.STATE_PANELCOUNT, myFrameCount, oldPanelCount });
    if (oldPanelCount == myFrameCount) {
        // Il numero di pannelli non e' cambiato:
        // Il sistema ha gia' ripristinato correttamente i frammenti.
        return;
    }

    fragmentQueue = new ArrayDeque<FragmentSupport>();
    fragmentCollector.collectFragments(fragmentQueue);

    // Ad ogni frammento associo il tag con il quale &egrave; stato
    // inserito
    clonedQueue = new ArrayDeque<FragmentLayoutController.FragmentEntry>();
    while (!fragmentQueue.isEmpty()) {
        fragment = fragmentQueue.remove();
        if (fragment == null) {
            myLogger.warn("Ignoring null.");
            continue;
        }

        tag = fragment.asFragment().getTag();
        if (StringUtils.isBlank(tag)) {
            myLogger.warn("Ignoring fragment with empty tag.");
            continue;
        }

        clonedQueue.offer(new FragmentLayoutController.FragmentEntry(fragment.cloneFragment(), tag));
    }

    fragmentQueue = null; // free memory

    activitySupport = (ActivitySupport) myActivity;
    fragmentMgr = activitySupport.getSupportFragmentManager();

    // Ripristino la configurazione dei frammenti iniziale
    for (n = fragmentMgr.getBackStackEntryCount(); n > 0; n--) {
        fragmentMgr.popBackStack();
    }

    if (myFrameCount > 1) {
        tnId = arrangeFragments(fragmentMgr, clonedQueue);
    } else {
        tnId = arrangePanel(fragmentMgr, clonedQueue);
    }

    if (Build.VERSION.SDK_INT < BuildCompat.VERSION_CODES.HONEYCOMB) {
        return;
    }

    // - Android 4.1.2
    // La barra delle azioni non e' correttamente aggiornata forse perche'
    // si assume che non ce ne sia bisogno con transazioni schedulate
    // durante il ripristino dell'attivita' (o magari perche' non e' proprio
    // previsto che si schedulino transazioni durante il ripristino
    // dell'attivita'):
    // Visto che l'esecuzione delle transazioni e' asincrona, devo
    // utilizzare un gestore degli eventi di modifica del back stack che
    // gestisca l&rsquo;ultima transazione che ho schedulato.
    backStackListener = new FragmentLayoutController.BackStackChangedListener(myActivity, fragmentMgr, tnId);
    fragmentMgr.addOnBackStackChangedListener(backStackListener);
}

From source file:com.bernard.beaconportal.activities.activity.MessageList.java

private void initializeFragments() {
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.addOnBackStackChangedListener(this);

    boolean hasMessageListFragment = (mMessageListFragment != null);

    if (!hasMessageListFragment) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        mMessageListFragment = MessageListFragment.newInstance(mSearch, false,
                (K9.isThreadedViewEnabled() && !mNoThreading));
        ft.add(R.id.message_list_container, mMessageListFragment);
        ft.commit();/*from   www.  j  a v  a 2s .co  m*/
    }

    // Check if the fragment wasn't restarted and has a MessageReference in
    // the arguments. If
    // so, open the referenced message.
    if (!hasMessageListFragment && mMessageViewFragment == null && mMessageReference != null) {
        openMessage(mMessageReference);
    }
}

From source file:com.bernard.beaconportal.activities.activity.MessageList.java

/**
 * Create fragment instances if necessary.
 * /*from w  w w  .ja v a2s .c o m*/
 * @see #findFragments()
 */

private void removeFragments() {
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.addOnBackStackChangedListener(this);

    boolean hasMessageListFragment = (mMessageListFragment != null);

    if (!hasMessageListFragment) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        mMessageListFragment = MessageListFragment.newInstance(mSearch, false,
                (K9.isThreadedViewEnabled() && !mNoThreading));
        // ft.remove(mMessageListFragment);
        ft.commit();

        Log.d("removed fragment?", "yes");
    }

    // Check if the fragment wasn't restarted and has a MessageReference in
    // the arguments. If
    // so, open the referenced message.
    if (!hasMessageListFragment && mMessageViewFragment == null && mMessageReference != null) {
        openMessage(mMessageReference);
    }
}