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.achep.acdisplay.acdisplay.AcDisplayActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    mMediaController = new MediaController();
    mMediaController.onCreate(this);

    mTimeout = new Timeout();
    mTimeout.registerListener(this);

    super.onCreate(savedInstanceState);

    if (mConfig.isWallpaperShown()) {
        if (mConfig.isShadowEnabled()) {
            setTheme(R.style.AcDisplayTheme_Wallpaper_WithShadow);
        } else {//from ww w  . j  a v  a  2 s . co m
            setTheme(R.style.AcDisplayTheme_Wallpaper);
        }
    }

    setContentView(R.layout.acdisplay);
    mBackgroundView = (ImageView) findViewById(R.id.background);

    // Initialize non-UI fragments.
    if (savedInstanceState == null) {
        initInternalFragments();
    } else {

        // Find fragments.
        FragmentManager fm = getFragmentManager();
        mPocketFragment = (PocketFragment) fm.findFragmentByTag(PocketFragment.TAG);
        // TODO: Maybe remove PocketFragment if active mode is disabled?
    }

    // Setup fragments.
    if (mPocketFragment != null) {
        mPocketFragment.setListener(this);
    }

    Presenter.getInstance().attachActivity(this);
}

From source file:com.example.android.cloudnotes.ui.HomeActivity.java

/**
 * This method controls both fragments, instructing them to display a
 * certain note.//from   ww w  .j  a  v  a  2 s.  c om
 * 
 * @param noteUri The {@link Uri} of the note to show. To create a new note,
 *            pass {@code null}.
 */
private void showNote(final Uri noteUri) {

    if (mTwoPaneView) {
        // check if the NoteEditFragment has been added
        FragmentManager fm = getFragmentManager();
        NoteEditFragment edit = (NoteEditFragment) fm.findFragmentByTag(NOTE_EDIT_TAG);
        final boolean editNoteAdded = (edit != null);

        if (editNoteAdded) {
            if (edit.mCurrentNote != null && edit.mCurrentNote.equals(noteUri)) {
                // clicked on the currently selected note
                return;
            }

            NoteEditFragment editFrag = (NoteEditFragment) fm.findFragmentByTag(NOTE_EDIT_TAG);
            if (noteUri != null) {
                // load an existing note
                editFrag.loadNote(noteUri);
                NoteListFragment list = (NoteListFragment) fm.findFragmentById(R.id.list);
                list.setActivatedNote(Long.valueOf(noteUri.getLastPathSegment()));
            } else {
                // creating a new note - clear the form & list
                // activation
                if (editNoteAdded) {
                    editFrag.clear();
                }
                NoteListFragment list = (NoteListFragment) fm.findFragmentById(R.id.list);
                list.clearActivation();
            }
        } else {
            // add the NoteEditFragment to the container
            FragmentTransaction ft = fm.beginTransaction();
            edit = new NoteEditFragment();
            ft.add(R.id.note_detail_container, edit, NOTE_EDIT_TAG);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            ft.commit();
            edit.loadNote(noteUri);
        }
    } else {
        startActivity(new Intent(NoteEditFragment.ACTION_VIEW_NOTE, noteUri));
    }
}

From source file:com.bradbergeron.splitviewcontrollerdemo.activities.MainActivity.java

@Override
public void onDrawerItemSelected(final DrawerFragment.FragmentDrawerItem fragmentDrawerItem) {
    if (mDrawerLayout.isDrawerOpen(Gravity.START)) {
        mDrawerLayout.closeDrawer(Gravity.START);
    }/*  w w w. j ava2s .com*/

    final FragmentManager fragmentManager = getFragmentManager();
    final String fragmentClassName = fragmentDrawerItem.getFragmentClass().getName();

    Fragment newFragment = fragmentManager.findFragmentByTag(fragmentClassName);

    if (newFragment == null) {
        newFragment = Fragment.instantiate(this, fragmentClassName);
    } else if (newFragment.isAdded()) {
        return;
    } else if (fragmentDrawerItem.isDefaultItem() && fragmentManager.getBackStackEntryCount() > 0) {
        fragmentManager.popBackStack("Drawer", FragmentManager.POP_BACK_STACK_INCLUSIVE);
    }

    final FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.content, newFragment, fragmentClassName);

    if (!fragmentDrawerItem.isDefaultItem()) {
        transaction.addToBackStack("Drawer");
    }

    transaction.commit();
}

From source file:android.support.v17.leanback.app.GuidedStepFragment.java

/**
 * Adds the specified GuidedStepFragment as content of Activity; no backstack entry is added so
 * the activity will be dismissed when BACK key is pressed.  The method is typically called in
 * Activity.onCreate() when savedInstanceState is null.  When savedInstanceState is not null,
 * the Activity is being restored,  do not call addAsRoot() to duplicate the Fragment restored
 * by FragmentManager.//from  w w  w  . java2 s  .c  o m
 * {@link #UI_STYLE_ACTIVITY_ROOT} is assigned.
 *
 * Note: currently fragments added using this method must be created programmatically rather
 * than via XML.
 * @param activity The Activity to be used to insert GuidedstepFragment.
 * @param fragment The GuidedStepFragment to be inserted into the fragment stack.
 * @param id The id of container to add GuidedStepFragment, can be android.R.id.content.
 * @return The ID returned by the call FragmentTransaction.commit, or -1 there is already
 *         GuidedStepFragment.
 */
public static int addAsRoot(Activity activity, GuidedStepFragment fragment, int id) {
    // Workaround b/23764120: call getDecorView() to force requestFeature of ActivityTransition.
    activity.getWindow().getDecorView();
    FragmentManager fragmentManager = activity.getFragmentManager();
    if (fragmentManager.findFragmentByTag(TAG_LEAN_BACK_ACTIONS_FRAGMENT) != null) {
        Log.w(TAG, "Fragment is already exists, likely calling "
                + "addAsRoot() when savedInstanceState is not null in Activity.onCreate().");
        return -1;
    }
    FragmentTransaction ft = fragmentManager.beginTransaction();
    fragment.setUiStyle(UI_STYLE_ACTIVITY_ROOT);
    return ft.replace(id, fragment, TAG_LEAN_BACK_ACTIONS_FRAGMENT).commit();
}

From source file:am.project.x.business.main.MainActivity.java

private void setFragment(String tag) {
    final String oldTag = mCurrent;
    mCurrent = tag;/*from www  . jav a  2  s .c om*/
    final FragmentManager manager = getFragmentManager();
    final FragmentTransaction transaction = manager.beginTransaction();
    if (!TextUtils.isEmpty(oldTag)) {
        final Fragment old = manager.findFragmentByTag(oldTag);
        if (old != null && old.isVisible()) {
            transaction.hide(old);
        }
    }
    Fragment target = manager.findFragmentByTag(tag);
    if (target != null && target.isVisible())
        return;
    if (target == null) {
        switch (tag) {
        default:
        case TAG_WIDGETS:
            target = WidgetsFragment.newInstance();
            break;
        case TAG_DRAWABLES:
            target = DrawablesFragment.newInstance();
            break;
        case TAG_OTHERS:
            target = OthersFragment.newInstance();
            break;
        case TAG_DEVELOP:
            target = DevelopFragment.newInstance();
            break;
        }
        transaction.add(mVContent.getId(), target, tag);
    }
    transaction.show(target);
    transaction.commit();
}

From source file:com.facebook.react.modules.timepicker.TimePickerDialogModule.java

@ReactMethod
public void open(@Nullable final ReadableMap options, Promise promise) {

    Activity activity = getCurrentActivity();
    if (activity == null) {
        promise.reject(ERROR_NO_ACTIVITY,
                "Tried to open a TimePicker dialog while not attached to an Activity");
        return;//  ww w.j  a  va  2 s.co  m
    }
    // We want to support both android.app.Activity and the pre-Honeycomb FragmentActivity
    // (for apps that use it for legacy reasons). This unfortunately leads to some code duplication.
    if (activity instanceof android.support.v4.app.FragmentActivity) {
        android.support.v4.app.FragmentManager fragmentManager = ((android.support.v4.app.FragmentActivity) activity)
                .getSupportFragmentManager();
        android.support.v4.app.DialogFragment oldFragment = (android.support.v4.app.DialogFragment) fragmentManager
                .findFragmentByTag(FRAGMENT_TAG);
        if (oldFragment != null) {
            oldFragment.dismiss();
        }
        SupportTimePickerDialogFragment fragment = new SupportTimePickerDialogFragment();
        if (options != null) {
            Bundle args = createFragmentArguments(options);
            fragment.setArguments(args);
        }
        TimePickerDialogListener listener = new TimePickerDialogListener(promise);
        fragment.setOnDismissListener(listener);
        fragment.setOnTimeSetListener(listener);
        fragment.show(fragmentManager, FRAGMENT_TAG);
    } else {
        FragmentManager fragmentManager = activity.getFragmentManager();
        DialogFragment oldFragment = (DialogFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG);
        if (oldFragment != null) {
            oldFragment.dismiss();
        }
        TimePickerDialogFragment fragment = new TimePickerDialogFragment();
        if (options != null) {
            final Bundle args = createFragmentArguments(options);
            fragment.setArguments(args);
        }
        TimePickerDialogListener listener = new TimePickerDialogListener(promise);
        fragment.setOnDismissListener(listener);
        fragment.setOnTimeSetListener(listener);
        fragment.show(fragmentManager, FRAGMENT_TAG);
    }
}

From source file:com.ppgllrd.alfil.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.d("ppgllrd", "onCreate: " + savedInstanceState);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_double);

    DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    ListView drawerList = (ListView) findViewById(R.id.drawer_list);

    Course[] courses = Course.getCourses();
    String currentYear = "";
    drawerItems = new ArrayList<DrawerItem>();
    for (Course course : courses) {
        if (course.getYear().compareTo(currentYear) != 0) {
            currentYear = course.getYear();
            drawerItems.add(new DrawerSection(currentYear));
        }/*from w w w  .j a  va 2s. c  om*/
        drawerItems.add(new DrawerCourse(course));
    }

    // set up the drawer's list view with items and click listener
    drawerList.setAdapter(new DrawerAdapter(this, R.layout.drawer_course_item, drawerItems));

    drawerList.setOnItemClickListener(new DrawerItemClickListener());

    // enable ActionBar app icon to behave as action to toggle nav drawer
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    actionBarTitleController = new ActionBarTitleController(this, /* host Activity */
            drawerLayout, /* DrawerLayout object */
            R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
            R.string.drawer_open, /* "open drawer" description for accessibility */
            R.string.drawer_close /* "close drawer" description for accessibility */
    );

    FragmentManager fm = getFragmentManager();

    drawerSelectedIdx = 1; // start by selecting first course
    if (fm.findFragmentByTag(StudentsListFragment.FragmentTag) == null) {
        DrawerCourse drawerCourse = (DrawerCourse) drawerItems.get(drawerSelectedIdx);
        studentsListFragment = new StudentsListFragment();
        Bundle args = new Bundle();
        args.putParcelable(StudentsListFragment.ARG_GROUP_STUDENTS_COURSE, drawerCourse.getCourse());
        studentsListFragment.setArguments(args);

        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.list_Fragment_Placeholder, studentsListFragment, StudentsListFragment.FragmentTag);
        ft.commit();
    } else {
        studentsListFragment = (StudentsListFragment) getFragmentManager()
                .findFragmentByTag(StudentsListFragment.FragmentTag);
    }

    if (fm.findFragmentByTag(StudentInfoFragment.FragmentTag) == null) {
        studentInfoFragment = new StudentInfoFragment();
        Bundle args = new Bundle();
        args.putParcelable(StudentInfoFragment.ARG_STUDENT,
                new Student(((DrawerCourse) drawerItems.get(drawerSelectedIdx)).getCourse()));
        studentInfoFragment.setArguments(args);

        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.info_Fragment_Placeholder, studentInfoFragment, StudentInfoFragment.FragmentTag);
        ft.commit();
    } else {
        studentInfoFragment = (StudentInfoFragment) getFragmentManager()
                .findFragmentByTag(StudentInfoFragment.FragmentTag);
    }

    FragmentTransaction ft = fm.beginTransaction();
    ft.hide(studentInfoFragment);
    ft.show(studentsListFragment);
    //ft.addToBackStack(null);
    ft.commit();

    // ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(this);
    // DrawerLayout lowestLayout = (DrawerLayout)this.findViewById(R.id.drawer_layout);
    // lowestLayout.setOnTouchListener(activitySwipeDetector);

}

From source file:com.facebook.react.modules.datepicker.DatePickerDialogModule.java

/**
 * Show a date picker dialog./*from   w w w.  j  av  a  2  s .c o m*/
 *
 * @param options a map containing options. Available keys are:
 *
 * <ul>
 *   <li>{@code date} (timestamp in milliseconds) the date to show by default</li>
 *   <li>
 *     {@code minDate} (timestamp in milliseconds) the minimum date the user should be allowed
 *     to select
 *   </li>
 *   <li>
 *     {@code maxDate} (timestamp in milliseconds) the maximum date the user should be allowed
 *     to select
 *    </li>
 * </ul>
 *
 * @param promise This will be invoked with parameters action, year,
 *                month (0-11), day, where action is {@code dateSetAction} or
 *                {@code dismissedAction}, depending on what the user did. If the action is
 *                dismiss, year, month and date are undefined.
 */
@ReactMethod
public void open(@Nullable final ReadableMap options, Promise promise) {
    Activity activity = getCurrentActivity();
    if (activity == null) {
        promise.reject(ERROR_NO_ACTIVITY,
                "Tried to open a DatePicker dialog while not attached to an Activity");
        return;
    }
    // We want to support both android.app.Activity and the pre-Honeycomb FragmentActivity
    // (for apps that use it for legacy reasons). This unfortunately leads to some code duplication.
    if (activity instanceof android.support.v4.app.FragmentActivity) {
        android.support.v4.app.FragmentManager fragmentManager = ((android.support.v4.app.FragmentActivity) activity)
                .getSupportFragmentManager();
        android.support.v4.app.DialogFragment oldFragment = (android.support.v4.app.DialogFragment) fragmentManager
                .findFragmentByTag(FRAGMENT_TAG);
        if (oldFragment != null) {
            oldFragment.dismiss();
        }
        SupportDatePickerDialogFragment fragment = new SupportDatePickerDialogFragment();
        if (options != null) {
            final Bundle args = createFragmentArguments(options);
            fragment.setArguments(args);
        }
        final DatePickerDialogListener listener = new DatePickerDialogListener(promise);
        fragment.setOnDismissListener(listener);
        fragment.setOnDateSetListener(listener);
        fragment.show(fragmentManager, FRAGMENT_TAG);
    } else {
        FragmentManager fragmentManager = activity.getFragmentManager();
        DialogFragment oldFragment = (DialogFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG);
        if (oldFragment != null) {
            oldFragment.dismiss();
        }
        DatePickerDialogFragment fragment = new DatePickerDialogFragment();
        if (options != null) {
            final Bundle args = createFragmentArguments(options);
            fragment.setArguments(args);
        }
        final DatePickerDialogListener listener = new DatePickerDialogListener(promise);
        fragment.setOnDismissListener(listener);
        fragment.setOnDateSetListener(listener);
        fragment.show(fragmentManager, FRAGMENT_TAG);
    }
}

From source file:com.reallynourl.nourl.fmpfoldermusicplayer.ui.activity.MainActivity.java

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    FragmentManager fm = getFragmentManager();
    IMainContent fragment = (IMainContent) fm.findFragmentByTag(CONTENT_FRAGMENT_TAG);
    if (fragment != null)
        mActiveContent = fragment;// ww  w . j av  a  2  s . c o m
    switch (id) {
    case R.id.nav_file_browser:
        if (mActiveContent == null || !mActiveContent.getName().equals(FileBrowserFragment.NAME)
                || !mActiveContent.isCreated()) {
            if (mActiveContentSavedName == null || !mActiveContentSavedName.equals(FileBrowserFragment.NAME)) {
                loadFragmentToContent(new FileBrowserFragment());
                Log.d("Navigation", "Loading file browser fragment!");
            }
        }
        break;
    case R.id.nav_settings:
        if (mActiveContent == null || !mActiveContent.getName().equals(SettingsFragment.NAME)) {
            if (mActiveContentSavedName == null || !mActiveContentSavedName.equals(SettingsFragment.NAME)) {
                loadFragmentToContent(new SettingsFragment());
                Log.d("Navigation", "Loading Settings fragment");
            }
        }
        break;
    case R.id.nav_currently_playing:
        if (mActiveContent == null || !mActiveContent.getName().equals(MusicPlayingFragment.NAME)) {
            if (mActiveContentSavedName == null || !mActiveContentSavedName.equals(MusicPlayingFragment.NAME)) {
                loadFragmentToContent(new MusicPlayingFragment());
                Log.d("Navigation", "Loading Player fragment");
            }
        }
        break;
    default:
        Log.e("Navigation", "Navigation button has no action, id: " + id + " text: " + item.getTitle());
    }

    mActiveContentSavedName = null;
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}