Example usage for android.app FragmentTransaction add

List of usage examples for android.app FragmentTransaction add

Introduction

In this page you can find the example usage for android.app FragmentTransaction add.

Prototype

public abstract FragmentTransaction add(@IdRes int containerViewId, Fragment fragment, String tag);

Source Link

Document

Add a fragment to the activity state.

Usage

From source file:com.google.android.apps.forscience.whistlepunk.LicenseActivity.java

private void showLicense(License license) {
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.add(R.id.container, LicenseFragment.newInstance(license), "license");
    ft.addToBackStack(license.key);//from ww  w .j  av a  2s .  c om
    ft.commit();
}

From source file:com.google.android.apps.forscience.whistlepunk.LicenseActivity.java

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

    setContentView(R.layout.activity_licenses);

    if (getFragmentManager().findFragmentByTag("list") == null) {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(R.id.container, new LicenseListFragment(), "list");
        ft.commit();//from  w  ww. j  av  a2 s  . com
    }

    final ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}

From source file:com.vst.functionframework.ui.SupportActivity.java

/**
 * Fragment?/*from  www .  j  av  a2 s  .co m*/
 *
 * @param resView        ??
 * @param targetFragment ??Fragment
 */
public void changeFragment(int resView, VstFragment targetFragment) {
    if (targetFragment.equals(currentKJFragment)) {
        return;
    }
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    if (!targetFragment.isAdded()) {
        transaction.add(resView, targetFragment, targetFragment.getClass().getName());
    }
    if (targetFragment.isHidden()) {
        transaction.show(targetFragment);
        targetFragment.onChange();
    }
    if (currentKJFragment != null && currentKJFragment.isVisible()) {
        transaction.hide(currentKJFragment);
    }
    currentKJFragment = targetFragment;
    transaction.commit();
}

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  v a 2s.  co  m*/
        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.vst.functionframework.ui.SupportActivity.java

/**
 * Fragment?//from   ww  w .j  a va 2s.  co m
 *
 * @param resView        ??
 * @param targetFragment ??Fragment
 */
public void changeFragment(int resView, SupportFragment targetFragment) {
    if (targetFragment.equals(currentSupportFragment)) {
        return;
    }
    android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    if (!targetFragment.isAdded()) {
        transaction.add(resView, targetFragment, targetFragment.getClass().getName());
    }
    if (targetFragment.isHidden()) {
        transaction.show(targetFragment);
        targetFragment.onChange();
    }
    if (currentSupportFragment != null && currentSupportFragment.isVisible()) {
        transaction.hide(currentSupportFragment);
    }
    currentSupportFragment = targetFragment;
    transaction.commit();
}

From source file:la.marsave.fullscreentest.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WindowManager.LayoutParams layout = getWindow().getAttributes();
    layout.screenBrightness = 1F;/*from w ww .j  a  v a  2s.  co m*/
    getWindow().setAttributes(layout);
    setContentView(R.layout.activity_main);

    getFragmentManager().addOnBackStackChangedListener(this);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    /*
      The {@link android.support.v4.view.PagerAdapter} that will provide
      fragments for each of the sections. We use a
      {@link SectionsPagerAdapter} derivative, which will keep every
      loaded fragment in memory. If this becomes too memory intensive, it
      may be best to switch to a
      {@link android.support.v13.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());

    mScrollDetector = new GestureDetector(this, new ViewPagerGestureDetector());

    // Set up the ViewPager with the sections adapter.
    mInfiniteViewPager = (InfiniteViewPager) findViewById(R.id.pager);
    mInfiniteViewPager.setAdapter(new InfinitePagerAdapter(mSectionsPagerAdapter));
    mInfiniteViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
    mInfiniteViewPager.setOnTouchListener(mTouchViewPagerListener);

    // Set up the DarkHover that will be seen to cover the adapter once slided in.
    mDarkHoverView = findViewById(R.id.dark_hover_view);
    mDarkHoverView.setAlpha(0);
    mDarkHoverView.setOnTouchListener(mTouchDarkHoverListener);

    mTextFragment = new TextFragment();
    mTextFragment.setOnTextFragmentAnimationEnd(this);

    //we look for the help fragment if already exists.
    mHelpFragment = (HelpFragment) getFragmentManager().findFragmentByTag(HELPED);

    //If it doesn't, we create it, if needed.
    if (mHelpFragment == null
            && !getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE).getBoolean(HELPED, false)) {
        mHelpFragment = new HelpFragment();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.add(R.id.move_to_back_container, mHelpFragment, HELPED);
        transaction.commit();
    }
    if (mHelpFragment != null) {
        mDarkHoverView.setAlpha(0.7F);
        mHelpFragment.setClickListener(mClickHelper);
    }
}

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

/**
 * This method controls both fragments, instructing them to display a
 * certain note.//w w w  .j  av  a 2  s.c  o  m
 * 
 * @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:org.chromium.chrome.browser.bookmark.ManageBookmarkActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {/*  w w  w  .  j a  va 2  s.  co  m*/
        if (getApplication() instanceof ChromeApplication) {
            ((ChromeApplication) getApplication()).startBrowserProcessesAndLoadLibrariesSync(true);
        }
    } catch (ProcessInitException e) {
        Log.e(TAG, "Unable to load native library.", e);
        ChromeApplication.reportStartupErrorAndExit(e);
        return;
    }
    if (!DeviceFormFactor.isTablet(this)) {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN
                | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }

    if (savedInstanceState == null) {
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.add(android.R.id.content, generateBaseFragment(), BASE_ADD_EDIT_FRAGMENT_TAG);
        fragmentTransaction.commit();
    } else {
        initializeFragmentState();
    }

    // When adding or removing fragments, ensure the keyboard is hidden if visible as the
    // editing fields are no longer on the screen.
    getFragmentManager().addOnBackStackChangedListener(new OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            UiUtils.hideKeyboard(findViewById(android.R.id.content));
        }
    });
    if (checkPermission(Manifest.permission.NFC, Process.myPid(),
            Process.myUid()) == PackageManager.PERMISSION_GRANTED) {
        NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (nfcAdapter != null)
            nfcAdapter.setNdefPushMessage(null, this);
    }
}

From source file:org.jp.liteframe.LiteActivity.java

/**
 * Fragment?//from w ww  . java  2  s.c  o m
 *
 * @param resView        ??
 * @param targetFragment ??Fragment
 */
public void changeFragment(int resView, LiteFragment targetFragment) {
    if (targetFragment.equals(mCurrentLiteFragment)) {
        return;
    }
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    if (!targetFragment.isAdded()) {
        transaction.add(resView, targetFragment, targetFragment.getClass().getName());
    }
    if (targetFragment.isHidden()) {
        transaction.show(targetFragment);
        targetFragment.onChange();
    }
    if (mCurrentLiteFragment != null && mCurrentLiteFragment.isVisible()) {
        transaction.hide(mCurrentLiteFragment);
    }
    mCurrentLiteFragment = targetFragment;
    transaction.commit();
}

From source file:com.breadwallet.presenter.activities.IntroActivity.java

private void createFragments() {
    introWelcomeFragment = new IntroWelcomeFragment();
    introNewRecoverFragment = new IntroNewRecoverFragment();
    introNewWalletFragment = new IntroNewWalletFragment();
    introRecoverWalletFragment = new IntroRecoverWalletFragment();
    introWarningFragment = new IntroWarningFragment();

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

    fragmentTransaction.add(R.id.intro_layout, introWelcomeFragment, IntroWelcomeFragment.class.getName());
    fragmentTransaction.add(R.id.intro_layout, introNewRecoverFragment,
            IntroNewRecoverFragment.class.getName());
    fragmentTransaction.add(R.id.intro_layout, introNewWalletFragment, IntroNewWalletFragment.class.getName());
    fragmentTransaction.add(R.id.intro_layout, introRecoverWalletFragment,
            IntroRecoverWalletFragment.class.getName());
    fragmentTransaction.add(R.id.intro_layout, introWarningFragment, IntroWarningFragment.class.getName());

    showHideFragments(introWelcomeFragment);
    newRecoverNoneFlag = 0;/*from   w  w w  .  j  a  v  a  2 s  . c o  m*/
    fragmentTransaction.commitAllowingStateLoss();
}