Example usage for android.app FragmentManager beginTransaction

List of usage examples for android.app FragmentManager beginTransaction

Introduction

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

Prototype

public abstract FragmentTransaction beginTransaction();

Source Link

Document

Start a series of edit operations on the Fragments associated with this FragmentManager.

Usage

From source file:com.rastating.droidbeard.MainActivity.java

@Override
public void onNavigationDrawerItemSelected(int position) {
    Fragment fragment = null;/*  w  w  w  . j  a  va  2  s.co  m*/
    Preferences preferences = new Preferences(this);

    String sickbeardUrl = preferences.getSickbeardUrl();
    boolean hasUrl = sickbeardUrl != null && sickbeardUrl.length() > 0;
    boolean hasApiKey = preferences.getApiKey() != null && preferences.getApiKey().length() > 0;

    if ((!hasUrl || !hasApiKey) && position != 4 && position != 99) {
        fragment = new SetupFragment();
    } else if (!isUrlValid(sickbeardUrl) && position != 99 && position != 4) {
        fragment = new InvalidAddressFragment();
    } else if (position == 0) {
        if (mShowsFragment == null) {
            mShowsFragment = new ShowsFragment();
        }

        fragment = mShowsFragment;
    } else if (position == 1) {
        if (mComingEpisodesFragment == null) {
            mComingEpisodesFragment = new ComingEpisodesFragment();
        }

        fragment = mComingEpisodesFragment;
    } else if (position == 2) {
        fragment = new HistoryFragment();
    } else if (position == 3) {
        fragment = new LogFragment();
    } else if (position == 4) {
        fragment = new ProfilesFragment();
    } else if (position == 5) {
        onNavigationDrawerItemSelected(99);
        setTitle(getString(R.string.action_settings));
    } else if (position == 99) {
        fragment = new PreferencesFragment();
    }

    if (fragment != null) {
        FragmentManager manager = this.getFragmentManager();
        manager.beginTransaction().replace(R.id.container, fragment).commit();
        setCurrentFragment(fragment);

        if (floatingActionsMenu != null) {
            if (fragment instanceof ShowsFragment) {
                floatingActionsMenu.setVisibility(View.VISIBLE);
                ((ShowsFragment) fragment).setFloatingActionMenu(floatingActionsMenu);
            } else {
                floatingActionsMenu.setVisibility(View.INVISIBLE);
            }
        }
    }
}

From source file:com.example.koppa.driverlicensev2.MainActivity.java

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

    switch (id) {
    case R.id.nav_admin_add:
        addCUQuestionFragment();/* w  w  w .  j  a v  a 2s.  com*/
        break;

    case R.id.nav_admin_edit:
        addQuestionListFragment();
        break;

    case R.id.nav_client_new_test:
        addTestLicenseFragment();
        break;
    case R.id.nav_admin_sign_out:
    case R.id.nav_client_sign_out:
        signout();
        break;
    case R.id.nav_client_statistics:
        addStatisticFragment();
        break;
    case R.id.nav_admin_users:
        addUserListFragment();
        break;
    case R.id.nav_client_practice:
        addPracticeFragment();
        break;

    case R.id.nav_admin_settings:
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        SettingsFragment settingsFragment = new SettingsFragment();
        fragmentTransaction.replace(R.id.fragment_container, settingsFragment);
        fragmentTransaction.commit();
        break;
    default:
        break;
    }

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

From source file:com.ternup.caddisfly.activity.MainActivity.java

/**
 * Display view fragment for selected nav drawer menu option
 *///from   w  ww  .  j a va  2s .  c om
public void displayView(int position, boolean addToBackStack) {

    int index = getCurrentFragmentIndex();

    if (index == position) {
        // requested fragment is already showing
        return;
    }

    Fragment fragment;
    showCheckUpdateOption = false;
    switch (position) {
    case Globals.HOME_SCREEN_INDEX:
        if (homeFragment == null) {
            homeFragment = new HomeFragment();
        }
        fragment = homeFragment;
        break;
    case Globals.LOCATION_LIST_SCREEN_INDEX:
        if (mLocationListFragment == null) {
            mLocationListFragment = new LocationListFragment();
        }
        fragment = mLocationListFragment;
        break;
    case Globals.CALIBRATE_SCREEN_INDEX:
        if (calibrateFragment == null) {
            calibrateFragment = new CalibrateFragment();
        }
        fragment = calibrateFragment;
        break;
    case Globals.SETTINGS_SCREEN_INDEX:
        showCheckUpdateOption = true;
        if (settingsFragment == null) {
            settingsFragment = new SettingsFragment();
        }
        fragment = settingsFragment;
        break;
    case Globals.HELP_SCREEN_INDEX:
        showCheckUpdateOption = true;
        if (helpFragment == null) {
            helpFragment = new HelpFragment();
        }
        fragment = helpFragment;
        break;
    case Globals.ABOUT_SCREEN_INDEX:
        showCheckUpdateOption = true;
        if (aboutFragment == null) {
            aboutFragment = new AboutItFragment();
        }
        fragment = aboutFragment;
        break;
    default:
        toggleTheme();
        return;
    }
    invalidateOptionsMenu();
    if (fragment != null) {

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.replace(R.id.container, fragment, String.valueOf(position));
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        if (addToBackStack) {
            ft.addToBackStack(null);
        }
        ft.commit();
    }
    mNavigationDrawerFragment.checkItem(position);
}

From source file:com.pac.performance.MainActivity.java

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

    if (mWidth == 1 || mHeight == 1) {
        Display display = getWindowManager().getDefaultDisplay();
        mWidth = display.getWidth();//w w  w.  ja v  a  2  s.com
        mHeight = display.getHeight();
    }

    setContentView(R.layout.activity_main);

    CPUChange = BatteryChange = AudioChange = VoltageChange = IOChange = MinFreeChange = VMChange = false;

    mFragments.clear();
    mFragmentNames.clear();

    // add CPU Fragment
    mFragments.add(new CPUFragment());
    mFragmentNames.add(getString(R.string.cpu));

    // add Battery Fragment
    if (Utils.exist(FAST_CHARGE) || Utils.exist(BLX)) {
        mFragments.add(new BatteryFragment());
        mFragmentNames.add(getString(R.string.battery));
    }

    // add Audio Fragment
    if (Utils.exist(FAUX_SOUND_CONTROL)) {
        mFragments.add(new AudioFragment());
        mFragmentNames.add(getString(R.string.audio));
    }

    // add Voltage Fragment
    if (Utils.exist(VoltageHelper.CPU_VOLTAGE) || Utils.exist(VoltageHelper.FAUX_VOLTAGE)) {
        mFragments.add(new VoltageFragment());
        mFragmentNames.add(getString(R.string.voltage));
    }

    // add IO Fragment
    mFragments.add(new IOFragment());
    mFragmentNames.add(getString(R.string.io));

    // add MinFree Fragment
    mFragments.add(new MinFreeFragment());
    mFragmentNames.add(getString(R.string.minfree));

    // add VM Fragment
    if (VMHelper.getVMValues().size() == VMHelper.getVMFiles().size()) {
        mFragments.add(new VMFragment());
        mFragmentNames.add(getString(R.string.vm));
    }

    // add Information Fragment
    mFragments.add(new InformationFragment());
    mFragmentNames.add(getString(R.string.information));

    Utils.saveString("kernelversion", Utils.getFormattedKernelVersion(), getApplicationContext());

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

    mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.fragment_list, mFragmentNames));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open,
            R.string.drawer_close) {
        public void onDrawerClosed(View view) {
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    actionBar = getActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

    actionBar.setListNavigationCallbacks(new ArrayAdapter<String>(actionBar.getThemedContext(),
            android.R.layout.simple_list_item_1, android.R.id.text1, mFragmentNames), this);

    if (savedInstanceState == null) {
        setPerm();

        Fragment fragment = new ViewPagerFragment();

        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
        selectItem(0);
    }

}

From source file:com.example.atsuto5.yahoo_rss_reader.MainActivity.java

@SuppressWarnings("StatementWithEmptyBody")
@Override/* w w w .j  a  v  a2  s .  c  o m*/
public boolean onNavigationItemSelected(MenuItem item) {

    int id = item.getItemId();

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

    if (id == R.id.maintopics_logo) {
        fragmentTransaction.replace(R.id.fragment_container, MainTopicsFragment.newInstance());
        fragmentTransaction.commit();
        mToolbar.setTitle("");
        mToolbar.setTitleMargin(140, 0, 0, 0);

    } else if (id == R.id.international_logo) {
        fragmentTransaction.replace(R.id.fragment_container, InternationalFragment.newInstance());
        fragmentTransaction.commit();
        mToolbar.setTitle("");
        mToolbar.setTitleMargin(320, 0, 0, 0);

    } else if (id == R.id.entertainment) {
        fragmentTransaction.replace(R.id.fragment_container, EntertainmentFragment.newInstance());
        fragmentTransaction.commit();
        mToolbar.setTitle("");
        mToolbar.setTitleMargin(260, 0, 0, 0);

    } else if (id == R.id.it_logo) {
        fragmentTransaction.replace(R.id.fragment_container, ItFragment.newInstance());
        fragmentTransaction.commit();
        mToolbar.setTitle("IT");
        mToolbar.setTitleMargin(340, 0, 0, 0);

    } else if (id == R.id.local_logo) {
        fragmentTransaction.replace(R.id.fragment_container, LocalFragment.newInstance());
        fragmentTransaction.commit();
        mToolbar.setTitle("");
        mToolbar.setTitleMargin(320, 0, 0, 0);

    } else if (id == R.id.domestic_logo) {
        fragmentTransaction.replace(R.id.fragment_container, DomesticFragment.newInstance());
        fragmentTransaction.commit();
        mToolbar.setTitle("");
        mToolbar.setTitleMargin(320, 0, 0, 0);

    } else if (id == R.id.economy_logo) {
        fragmentTransaction.replace(R.id.fragment_container, EconomyFragment.newInstance());
        fragmentTransaction.commit();
        mToolbar.setTitle("");
        mToolbar.setTitleMargin(320, 0, 0, 0);

    } else if (id == R.id.sports_logo) {
        fragmentTransaction.replace(R.id.fragment_container, SportsFragment.newInstance());
        fragmentTransaction.commit();
        mToolbar.setTitle("?");
        mToolbar.setTitleMargin(260, 0, 0, 0);

    } else if (id == R.id.science_logo) {
        fragmentTransaction.replace(R.id.fragment_container, ScienceFragment.newInstance());
        fragmentTransaction.commit();
        mToolbar.setTitle("");
        mToolbar.setTitleMargin(320, 0, 0, 0);

    } else if (id == R.id.interestgraph_logo) {
        fragmentTransaction.replace(R.id.fragment_container, InterestChartFragment.newInstance());
        fragmentTransaction.commit();
        mToolbar.setTitle("?????");
        mToolbar.setTitleMargin(200, 0, 0, 0);

    } else if (id == R.id.your_like_logo) {
        fragmentTransaction.replace(R.id.fragment_container, InterestChartFragment.newInstance());
        fragmentTransaction.commit();
        mToolbar.setTitle("?????");
        mToolbar.setTitleMargin(200, 0, 0, 0);

    } else if ("License".equals(item.getTitle())) {
        fragmentTransaction.replace(R.id.fragment_container, LicenseFragment.newInstance());
        fragmentTransaction.commit();
        mToolbar.setTitle("License");
        mToolbar.setTitleMargin(280, 0, 0, 0);

    } else if ("?".equals(item.getTitle())) {
        fragmentTransaction.replace(R.id.fragment_container, MainTopicsFragment.newInstance());
        fragmentTransaction.commit();
        mToolbar.setTitle("");
        mToolbar.setTitleMargin(140, 0, 0, 0);

    }

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

From source file:com.sbhstimetable.sbhs_timetable_android.TimetableActivity.java

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getFragmentManager();
    switch (position) { // USE THE BREAK, LUKE!
    case 0:/*from ww  w  .  ja v  a 2 s.co  m*/
        fragmentManager.beginTransaction().replace(R.id.container, CountdownFragment.newInstance()).commit();
        onMaster = 1;
        break;
    case 1:
        fragmentManager.beginTransaction().replace(R.id.container, TimetableFragment.newInstance()).commit();
        onMaster = 0;
        break;
    case 2:
        fragmentManager.beginTransaction().replace(R.id.container, NoticesFragment.newInstance()).commit();
        onMaster = 0;
        break;
    case 3:
        fragmentManager.beginTransaction().replace(R.id.container, BelltimesFragment.newInstance()).commit();
        onMaster = 0;
        break;
    case 4:
        if (!isActive)
            break; // don't do weirdness
        Log.i("timetableactivity", "isActive = false (launching SettingsActivity)");
        Intent settings = new Intent(this, SettingsActivity.class);
        isActive = false;
        this.startActivity(settings);
        break;
    case 5:
        if (ApiAccessor.isLoggedIn()) {
            ApiAccessor.logout(this);
            this.mNavigationDrawerFragment.updateList();
            Toast.makeText(this, "Logged out! (You may need to restart the app to remove all your data)",
                    Toast.LENGTH_SHORT).show();
            StorageCache.deleteAllCacheFiles(this);
            fragmentManager.beginTransaction().commit();
        } else {
            ApiAccessor.login(this);
        }
        break; // HAVE YOU GOT A PLAN BREAK?
    }
}

From source file:com.emman.tame.MainActivity.java

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
            .commit();/*  ww w  .j a va2  s. c o m*/
}

From source file:com.sevensales.MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);

    //   SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_websearch).getActionView();
    //        if (null != searchView) {
    //            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    //            searchView.setIconifiedByDefault(false);
    //        }/* w  w w  . ja v  a2  s .c  o m*/
    SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
        public boolean onQueryTextChange(String newText) {
            return true;
        }

        public boolean onQueryTextSubmit(String query) {
            storage.downloadSearchShops(query);
            SalesFragment fragment = new SalesFragment();
            fragment.sales = storage.getSalesList();

            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).addToBackStack(null)
                    .commit();
            return true;
        }
    };
    searchView.setOnQueryTextListener(queryTextListener);

    return super.onCreateOptionsMenu(menu);
}

From source file:cmput301.f13t01.readstory.ReadFragmentActivity.java

/**
 * Using a fragment manager, this function creates and commits a new
 * ReadFragmentView fragment to be displayed for this view. This replaces
 * whatever the old fragment is, thus giving the readers a new view on the
 * new content.//from   w  w w. jav a 2s  .  co  m
 * 
 * @param storyId
 *            the UUID of the story
 * @param fragmentId
 *            the story fragment to be displayed
 */
public void commitFragment(Integer fragmentId) {
    if (fragmentId == null) {
        return;
    }

    this.fragmentId = fragmentId;
    // prepare for the fragment
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    // prepare bundle to pass arguments
    Bundle bundle = new Bundle();
    bundle.putInt(getResources().getString(R.string.destination_id), fragmentId);

    // create the fragment and pass the bundle to the fragment
    ReadFragmentView newFragment = new ReadFragmentView();
    newFragment.setArguments(bundle);
    fragmentTransaction.replace(R.id.read_fragment_activity, newFragment);
    fragmentTransaction.commit();
}

From source file:com.royclarkson.springagram.MainActivity.java

@Override
public void onPhotoSelected(int position) {
    PhotoDetailFragment photoDetailFragment = PhotoDetailFragment.newInstance(position);
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction()
            .add(R.id.container, photoDetailFragment).addToBackStack(null);
    transaction.commit();/*  w w  w  . ja  va2s .  com*/
}