Example usage for android.app FragmentManager findFragmentByTag

List of usage examples for android.app FragmentManager findFragmentByTag

Introduction

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

Prototype

public abstract Fragment findFragmentByTag(String tag);

Source Link

Document

Finds a fragment that was identified by the given tag either when inflated from XML or as supplied when added in a transaction.

Usage

From source file:com.android.mail.ui.AbstractActivityController.java

/**
 * Use the instance variable and the wait fragment's tag to get the wait fragment.  This is
 * far superior to using the value of mWaitFragment, which might be invalid or might refer
 * to a fragment after it has been destroyed.
 * @return a wait fragment that is already attached to the activity, if one exists
 *///from   w  w  w. ja  v  a2 s  . co  m
protected final WaitFragment getWaitFragment() {
    final FragmentManager manager = mActivity.getFragmentManager();
    final WaitFragment waitFrag = (WaitFragment) manager.findFragmentByTag(TAG_WAIT);
    if (waitFrag != null) {
        // The Fragment Manager knows better, so use its instance.
        mWaitFragment = waitFrag;
    }
    return mWaitFragment;
}

From source file:com.lgallardo.qbittorrentclient.RefreshListener.java

private void selectItem(int position) {

    if (findViewById(R.id.one_frame) != null) {
        FragmentManager fragmentManager = getFragmentManager();

        if (fragmentManager.findFragmentByTag(
                "firstFragment") instanceof com.lgallardo.qbittorrentclient.TorrentDetailsFragment) {
            // Reset back button stack
            for (int i = 0; i < fragmentManager.getBackStackEntryCount(); ++i) {
                fragmentManager.popBackStack();
            }/*from   w  ww . j a  v a  2  s . c  o  m*/
        }
    }
}

From source file:com.android.mail.ui.AbstractActivityController.java

private void updateWaitMode() {
    final FragmentManager manager = mActivity.getFragmentManager();
    final WaitFragment waitFragment = (WaitFragment) manager.findFragmentByTag(TAG_WAIT);
    if (waitFragment != null) {
        waitFragment.updateAccount(mAccount);
    }//from   www  .  ja  v a  2  s . c  o  m
}

From source file:com.lgallardo.qbittorrentclient.RefreshListener.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get preferences
    getSettings();//from w ww .  j a  va  2s . c  om

    // Set alarm for checking completed torrents, if not set
    if (PendingIntent.getBroadcast(getApplication(), 0, new Intent(getApplication(), NotifierService.class),
            PendingIntent.FLAG_NO_CREATE) == null) {

        // Set Alarm for checking completed torrents
        alarmMgr = (AlarmManager) getApplication().getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(getApplication(), NotifierService.class);
        alarmIntent = PendingIntent.getBroadcast(getApplication(), 0, intent, 0);

        alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 5000,
                notification_period, alarmIntent);
    }

    // Set alarm for RSS checking, if not set
    if (PendingIntent.getBroadcast(getApplication(), 0, new Intent(getApplication(), RSSService.class),
            PendingIntent.FLAG_NO_CREATE) == null) {

        // Set Alarm for checking completed torrents
        alarmMgr = (AlarmManager) getApplication().getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(getApplication(), RSSService.class);
        alarmIntent = PendingIntent.getBroadcast(getApplication(), 0, intent, 0);

        alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 5000,
                AlarmManager.INTERVAL_DAY, alarmIntent);
    }

    // Set Theme (It must be fore inflating or setContentView)
    if (dark_ui) {
        this.setTheme(R.style.Theme_Dark);

        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().setNavigationBarColor(getResources().getColor(R.color.Theme_Dark_toolbarBackground));
            getWindow().setStatusBarColor(getResources().getColor(R.color.Theme_Dark_toolbarBackground));
        }
    } else {
        this.setTheme(R.style.Theme_Light);

        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().setNavigationBarColor(getResources().getColor(R.color.primary));
        }

    }

    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.app_bar);

    if (dark_ui) {
        toolbar.setBackgroundColor(getResources().getColor(R.color.Theme_Dark_primary));
    }

    setSupportActionBar(toolbar);

    // Set App title
    setTitle(R.string.app_shortname);

    // Drawer menu
    navigationDrawerServerItems = getResources().getStringArray(R.array.qBittorrentServers);
    navigationDrawerItemTitles = getResources().getStringArray(R.array.navigation_drawer_items_array);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    //        drawerList = (ListView) findViewById(R.id.left_drawer);

    mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView); // Assigning the RecyclerView Object to the xml View
    mRecyclerView.setHasFixedSize(true); // Letting the system know that the list objects are

    ArrayList<DrawerItem> serverItems = new ArrayList<DrawerItem>();
    ArrayList<DrawerItem> actionItems = new ArrayList<DrawerItem>();
    //        ArrayList<ObjectDrawerItem> labelItems = new ArrayList<ObjectDrawerItem>();
    ArrayList<DrawerItem> settingsItems = new ArrayList<DrawerItem>();

    // Add server category
    serverItems.add(new DrawerItem(R.drawable.ic_drawer_servers,
            getResources().getString(R.string.drawer_servers_category), DRAWER_CATEGORY, false, null));

    // Server items
    int currentServerValue = 1;

    try {
        currentServerValue = Integer.parseInt(MainActivity.currentServer);
    } catch (NumberFormatException e) {

    }

    for (int i = 0; i < navigationDrawerServerItems.length; i++) {
        serverItems.add(new DrawerItem(R.drawable.ic_drawer_subitem, navigationDrawerServerItems[i],
                DRAWER_ITEM_SERVERS, ((i + 1) == currentServerValue), "changeCurrentServer"));

    }

    // Add actions
    actionItems.add(new DrawerItem(R.drawable.ic_drawer_all, navigationDrawerItemTitles[0], DRAWER_ITEM_ACTIONS,
            lastState.equals("all"), "refreshAll"));
    actionItems.add(new DrawerItem(R.drawable.ic_drawer_downloading, navigationDrawerItemTitles[1],
            DRAWER_ITEM_ACTIONS, lastState.equals("downloading"), "refreshDownloading"));
    actionItems.add(new DrawerItem(R.drawable.ic_drawer_completed, navigationDrawerItemTitles[2],
            DRAWER_ITEM_ACTIONS, lastState.equals("completed"), "refreshCompleted"));
    actionItems.add(new DrawerItem(R.drawable.ic_drawer_seeding, navigationDrawerItemTitles[3],
            DRAWER_ITEM_ACTIONS, lastState.equals("seeding"), "refreshSeeding"));
    actionItems.add(new DrawerItem(R.drawable.ic_drawer_paused, navigationDrawerItemTitles[4],
            DRAWER_ITEM_ACTIONS, lastState.equals("pause"), "refreshPaused"));
    actionItems.add(new DrawerItem(R.drawable.ic_drawer_active, navigationDrawerItemTitles[5],
            DRAWER_ITEM_ACTIONS, lastState.equals("active"), "refreshActive"));
    actionItems.add(new DrawerItem(R.drawable.ic_drawer_inactive, navigationDrawerItemTitles[6],
            DRAWER_ITEM_ACTIONS, lastState.equals("inactive"), "refreshInactive"));

    // Add labels

    // Add settings actions
    settingsItems.add(new DrawerItem(R.drawable.ic_action_options, navigationDrawerItemTitles[7],
            DRAWER_ITEM_ACTIONS, false, "openOptions"));
    settingsItems.add(new DrawerItem(R.drawable.ic_drawer_settings, navigationDrawerItemTitles[8],
            DRAWER_ITEM_ACTIONS, false, "openSettings"));

    if (packageName.equals("com.lgallardo.qbittorrentclient")) {
        settingsItems.add(new DrawerItem(R.drawable.ic_drawer_pro, navigationDrawerItemTitles[9],
                DRAWER_ITEM_ACTIONS, false, "getPro"));
        settingsItems.add(new DrawerItem(R.drawable.ic_drawer_help, navigationDrawerItemTitles[10],
                DRAWER_ITEM_ACTIONS, false, "openHelp"));
    } else {
        settingsItems.add(new DrawerItem(R.drawable.ic_drawer_help, navigationDrawerItemTitles[9],
                DRAWER_ITEM_ACTIONS, false, "openHelp"));
    }

    rAdapter = new DrawerItemRecyclerViewAdapter(getApplicationContext(), this, serverItems, actionItems,
            settingsItems, null);
    rAdapter.notifyDataSetChanged();

    //        drawerList.setAdapter(adapter);
    mRecyclerView.setAdapter(rAdapter);

    mLayoutManager = new LinearLayoutManager(this); // Creating a layout Manager
    mRecyclerView.setLayoutManager(mLayoutManager); // Setting the layout Manager

    // Set selection according to last state
    setSelectionAndTitle(lastState);

    // Get drawer title
    title = drawerTitle = getTitle();

    // Add the application icon control code inside MainActivity onCreate

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    // New ActionBarDrawerToggle for Google Material Desing (v7)
    drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open,
            R.string.drawer_close) {

        /**
         * Called when a drawer has settled in a completely closed state.
         */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            // getSupportActionBar().setTitle(title);
        }

        /**
         * Called when a drawer has settled in a completely open state.
         */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            // getSupportActionBar().setTitle(drawerTitle);
            // setTitle(R.string.app_shortname);

        }
    };

    drawerLayout.setDrawerListener(drawerToggle);

    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    getSupportActionBar().setHomeButtonEnabled(false);

    // Get options and save them as shared preferences
    qBittorrentOptions qso = new qBittorrentOptions();
    qso.execute(new String[] { qbQueryString + "/preferences", "getSettings" });

    // If it was awoken from an intent-filter,
    // get intent from the intent filter and Add URL torrent
    addTorrentByIntent(getIntent());

    // Fragments

    // Check whether the activity is using the layout version with
    // the fragment_container FrameLayout. If so, we must add the first
    // fragment
    if (findViewById(R.id.fragment_container) != null) {

        // 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;
        // }

        // This fragment will hold the list of torrents
        if (firstFragment == null) {
            firstFragment = new com.lgallardo.qbittorrentclient.ItemstFragment();
        }

        // This fragment will hold the list of torrents
        helpTabletFragment = new HelpFragment();

        // Set the second fragments container
        firstFragment.setSecondFragmentContainer(R.id.content_frame);

        // This i the second fragment, holding a default message at the
        // beginning
        secondFragment = new AboutFragment();

        // Add the fragment to the 'list_frame' FrameLayout
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        if (fragmentManager.findFragmentByTag("firstFragment") == null) {
            fragmentTransaction.add(R.id.list_frame, helpTabletFragment, "firstFragment");
        } else {
            fragmentTransaction.replace(R.id.list_frame, helpTabletFragment, "firstFragment");
        }

        if (fragmentManager.findFragmentByTag("secondFragment") == null) {
            fragmentTransaction.add(R.id.content_frame, secondFragment, "secondFragment");
        } else {
            fragmentTransaction.replace(R.id.content_frame, secondFragment, "secondFragment");
        }

        fragmentTransaction.commit();

        // Second fragment will be added in ItemsFragment's onListItemClick method

    } else {

        // Phones handle just one fragment

        // Create an instance of ItemsFragments
        if (firstFragment == null) {
            firstFragment = new com.lgallardo.qbittorrentclient.ItemstFragment();
        }
        firstFragment.setSecondFragmentContainer(R.id.one_frame);

        // This is the about fragment, holding a default message at the
        // beginning
        secondFragment = new AboutFragment();

        // 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) {
        //
        //                // Handle Item list empty due to Fragment stack
        //                try {
        //                    FragmentManager fm = getFragmentManager();
        //
        //                    if (fm.getBackStackEntryCount() == 1 && fm.findFragmentById(R.id.one_frame) instanceof com.lgallardo.qbittorrentclient.TorrentDetailsFragment) {
        //
        //                        refreshCurrent();
        //
        //                    }
        //                }
        //                catch (Exception e) {
        //                }
        //
        //                return;
        //            }

        // Add the fragment to the 'list_frame' FrameLayout
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        if (fragmentManager.findFragmentByTag("firstFragment") == null) {
            fragmentTransaction.add(R.id.one_frame, secondFragment, "firstFragment");
        } else {
            fragmentTransaction.replace(R.id.one_frame, secondFragment, "firstFragment");
        }

        // if torrent details was loaded reset back button stack
        for (int i = 0; i < fragmentManager.getBackStackEntryCount(); ++i) {
            fragmentManager.popBackStack();
        }

        fragmentTransaction.commit();
    }

    // Activity is visible
    activityIsVisible = true;

    // First refresh
    refreshCurrent();

    handler = new Handler();
    handler.postDelayed(m_Runnable, refresh_period);

    // Load banner
    loadBanner();

}

From source file:com.lgallardo.youtorrentcontroller.RefreshListener.java

@Override
public void onResume() {
    super.onResume();
    activityIsVisible = true;/*from   w ww . j  a  v a  2  s. co  m*/

    // Handle Item list empty due to Fragment stack
    try {

        FragmentManager fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();

        if (fm.getBackStackEntryCount() == 0 && firstFragment.getSecondFragmentContainer() == R.id.one_frame
                && fm.findFragmentById(
                        R.id.one_frame) instanceof com.lgallardo.youtorrentcontroller.ItemstFragment) {

            com.lgallardo.youtorrentcontroller.ItemstFragment fragment = (com.lgallardo.youtorrentcontroller.ItemstFragment) fm
                    .findFragmentById(R.id.one_frame);

            if (fragment.getListView().getCount() == 0) {

                // Create the about fragment
                aboutFragment = new AboutFragment();

                fragmentTransaction.replace(R.id.one_frame, aboutFragment, "firstFragment");

                fragmentTransaction.commit();

                // Se titile
                //                    setTitle(navigationDrawerItemTitles[drawerList.getCheckedItemPosition()]);
                setTitle(navigationDrawerItemTitles[DrawerItemRecyclerViewAdapter.actionPosition]);

                // Close Contextual Action Bar
                if (firstFragment != null && firstFragment.mActionMode != null) {
                    firstFragment.mActionMode.finish();
                }

                // Refresh current list
                refreshCurrent();
            }

        }
        if (fm.getBackStackEntryCount() == 0 && firstFragment.getSecondFragmentContainer() == R.id.content_frame
                && (fm.findFragmentByTag("secondFragment") instanceof AboutFragment)) {

            // Create the about fragment
            aboutFragment = new AboutFragment();

            fragmentTransaction.replace(R.id.content_frame, aboutFragment, "secondFragment");

            fragmentTransaction.commit();

            // Set title
            setTitle(navigationDrawerItemTitles[DrawerItemRecyclerViewAdapter.actionPosition]);

            // Close Contextual Action Bar
            if (firstFragment != null && firstFragment.mActionMode != null) {
                firstFragment.mActionMode.finish();
            }

            // Refresh current list
            refreshCurrent();

        }
    } catch (Exception e) {

    }
}

From source file:com.lgallardo.qbittorrentclient.RefreshListener.java

@Override
public void onResume() {
    super.onResume();
    activityIsVisible = true;/*from  w  w w.ja v a  2s .com*/

    // Handle Item list empty due to Fragment stack
    try {

        FragmentManager fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();

        if (fm.getBackStackEntryCount() == 0 && firstFragment.getSecondFragmentContainer() == R.id.one_frame
                && fm.findFragmentById(
                        R.id.one_frame) instanceof com.lgallardo.qbittorrentclient.ItemstFragment) {

            com.lgallardo.qbittorrentclient.ItemstFragment fragment = (com.lgallardo.qbittorrentclient.ItemstFragment) fm
                    .findFragmentById(R.id.one_frame);

            if (fragment.getListView().getCount() == 0) {

                // Create the about fragment
                aboutFragment = new AboutFragment();

                fragmentTransaction.replace(R.id.one_frame, aboutFragment, "firstFragment");

                fragmentTransaction.commit();

                // Se title
                //                    setTitle(navigationDrawerItemTitles[drawerList.getCheckedItemPosition()]);
                //                    setTitle(navigationDrawerItemTitles[DrawerItemRecyclerViewAdapter.actionPosition]);
                setSelectionAndTitle(lastState);

                // Close Contextual Action Bar
                if (firstFragment != null && firstFragment.mActionMode != null) {
                    firstFragment.mActionMode.finish();
                }

                // Refresh current list
                refreshCurrent();
            }

        }
        if (fm.getBackStackEntryCount() == 0 && firstFragment.getSecondFragmentContainer() == R.id.content_frame
                && (fm.findFragmentByTag("secondFragment") instanceof AboutFragment)) {

            // Create the about fragment
            aboutFragment = new AboutFragment();

            fragmentTransaction.replace(R.id.content_frame, aboutFragment, "secondFragment");

            fragmentTransaction.commit();

            // Se title
            //                setTitle(navigationDrawerItemTitles[drawerList.getCheckedItemPosition()]);
            //                setTitle(navigationDrawerItemTitles[DrawerItemRecyclerViewAdapter.actionPosition]);
            setSelectionAndTitle(lastState);

            // Close Contextual Action Bar
            if (firstFragment != null && firstFragment.mActionMode != null) {
                firstFragment.mActionMode.finish();
            }

            // Refresh current list
            refreshCurrent();

        }
    } catch (Exception e) {

    }
}