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.example.koppa.driverlicensev2.MainActivity.java

public void addAdminFragment() {
    Fragment frag = null;//w w w  .j  ava 2 s . co m
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    NavigationView navigationView;
    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.inflateMenu(R.menu.nav_menu_admin);
    frag = new AdminFragment();
    toolbar.setVisibility(View.VISIBLE);

    fragmentTransaction.replace(R.id.fragment_container, frag);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

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

public void addClientFragment() {
    Fragment frag = null;/*from  ww  w .  j  ava2  s  . co  m*/
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    if (navigationView == null) {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.inflateMenu(R.menu.nav_menu_client);
        toolbar.setVisibility(View.VISIBLE);
    }

    frag = new ClientFragment();

    fragmentTransaction.replace(R.id.fragment_container, frag);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

}

From source file:com.samsunghack.apps.android.noq.NavDrawerMainActivity.java

private void selectItem(int position) {
    // update the main content by replacing fragments
    Fragment fragment = new PlanetFragment();
    Log.d(TAG, "+selectItem: positon = " + position);
    Bundle args = new Bundle();
    args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
    fragment.setArguments(args);// w  w w  .jav a  2  s .co m

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

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mAppFeatureTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

From source file:com.android.tv.settings.dialog.DialogFragment.java

public static void add(FragmentManager fm, DialogFragment f) {
    boolean hasDialog = fm.findFragmentByTag(TAG_LEAN_BACK_DIALOG_FRAGMENT) != null;
    FragmentTransaction ft = fm.beginTransaction();

    if (hasDialog) {
        ft.setCustomAnimations(ANIMATION_FRAGMENT_ENTER, ANIMATION_FRAGMENT_EXIT, ANIMATION_FRAGMENT_ENTER_POP,
                ANIMATION_FRAGMENT_EXIT_POP);
        ft.addToBackStack(null);//from w  ww  .  ja v a  2 s. co m
    }
    ft.replace(android.R.id.content, f, TAG_LEAN_BACK_DIALOG_FRAGMENT).commit();
}

From source file:com.nathanson.meterreader.activity.MainActivity.java

@Override
public void onNavigationDrawerItemSelected(int position) {

    // update the main content by replacing fragments
    FragmentManager fragmentManager = getFragmentManager();

    // use existing fragment, if possible.
    String fragmentTag = String.valueOf(position);
    Fragment frag = fragmentManager.findFragmentByTag(fragmentTag);

    fragmentManager.beginTransaction()
            .replace(R.id.container, frag != null ? frag : fragmentFactory(position), fragmentTag).commit();
}

From source file:com.example.admin.processingboilerplate.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
    FragmentManager fragmentManager = getFragmentManager();
    Fragment fragment = new Sketch();
    fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();

    // detect device type
    context = this.getApplicationContext();
}

From source file:com.cloudbees.gasp.activity.PlacesActivity.java

private void addPlacesFragments() {
    mSearchFragment = new NearbySearchFragment() {
        public void onSuccess(Places places) {
            showLocations(places);//from  w  w  w .  j ava2s.com
            checkToken(places);
        }

        public void onFailure(String status) {
            Log.e(TAG, "Google Places API search failed: status = " + status);
        }
    };

    mDetailsFragment = new PlaceDetailsFragment() {
        @Override
        public void onSuccess(PlaceDetails placeDetails) {
            showDetails(placeDetails);
        }

        @Override
        public void onFailure(String status) {
            Log.e(TAG, "Google Places API search failed: status = " + status);
        }
    };

    mAddEventFragment = new AddEventFragment() {
        @Override
        public void onSuccess(EventResponse eventResponse) {
            Log.d(TAG, "Event Added: " + eventResponse.getEvent_id());
        }

        @Override
        public void onFailure(String status) {
            Log.e(TAG, "Google Places API search failed: status = " + status);
        }
    };

    mDeleteEventFragment = new DeleteEventFragment() {
        @Override
        public void onSuccess(EventResponse eventResponse) {
            Log.d(TAG, "Event Deleted");
        }

        @Override
        public void onFailure(String status) {
            Log.e(TAG, "Google Places API search failed: status = " + status);
        }
    };

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(mSearchFragment, getString(R.string.fragment_location_search));
    ft.add(mDetailsFragment, getString(R.string.fragment_place_details));
    ft.add(mAddEventFragment, getString(R.string.fragment_add_event));
    ft.add(mDeleteEventFragment, getString(R.string.fragment_delete_event));
    ft.commit();
}

From source file:com.kyat.DJbooth.MainActivity.java

/**
 * Diplaying fragment view for selected nav drawer list item
 * *///from ww  w. j a  va2  s  . c om
private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;
    case 1:
        fragment = new SnapFragment();
        break;
    case 2:
        fragment = new PlannerFragment();
        break;
    case 3:
        fragment = new LeaderboardsFragment();
        break;
    case 4:
        fragment = new BadgesFragment();
        break;
    case 5:
        fragment = new VacanciesFragment();
        break;
    case 6:
        fragment = new AccountFragment();
        break;
    case 7:
        fragment = new SettingsFragment();
        break;
    case 8:
        fragment = new HelpFragment();
        break;

    default:
        break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

From source file:com.bulletingroupblast.bulletingroupblast.OrganizationActivity.java

/** The click event for the navigation drawer list items
 * open a fragment based on selection/* ww  w . j a v a  2s .  co  m*/
 * @param position navigation menu item selected index
 */
@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    android.app.Fragment fragment = null;
    Bundle args = new Bundle(); // For passing arguments

    // This is the click event switch for each menu item
    switch (position) {
    case 0:
        // Overview Fragment
        fragment = new OrganizationOverviewFragment();
        break;
    case 1:
        // Group list fragment
        fragment = new GroupItemFragment();
        break;
    case 2:
        // News list fragment
        fragment = new NewsItemFragment();
        break;
    case 3:
        // Events list fragment
        fragment = new EventItemFragment();
        break;
    case 4:
        // Calendar fragment
        fragment = new CalendarFragment();
        break;
    case 5:
        // Users fragment
        fragment = new UserItemFragment();
        break;
    default:
        // open the overview for default
        fragment = new OrganizationOverviewFragment();
        break;
    }

    // Check if fragment is valid
    if (fragment == null) {
        Log.e("OrganizationActivity", "Error in creating fragment");
    }

    // Pass organization id and name
    args.putInt(ORG_ID2, mOrgId);
    if (mOrganization != null) {
        args.putString(ORG_NAME, mOrganization.getName());
    }

    fragment.setArguments(args);
    FragmentManager fragmentManager = getFragmentManager();

    // Replace the fragment with selected fragment
    fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
}

From source file:com.orpheusdroid.screenrecorder.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);//w  w w  .ja  v  a2 s.c om

    //Arbitrary "Write to external storage" permission since this permission is most important for the app
    requestPermissionStorage();

    //final boolean isServiceRunning = isServiceRunning(RecorderService.class);
    //Let's add SettingsPreferenceFragment to the activity
    FragmentManager mFragmentManager = getFragmentManager();
    FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction();
    SettingsPreferenceFragment mPrefsFragment = new SettingsPreferenceFragment();
    mFragmentTransaction.replace(R.id.settingsFragment, mPrefsFragment);
    mFragmentTransaction.commit();

    //Acquiring media projection service to start screen mirroring
    mProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);

    //Respond to app shortcut
    if (getIntent().getAction() != null
            && getIntent().getAction().equals(getString(R.string.app_shortcut_action))) {
        startActivityForResult(mProjectionManager.createScreenCaptureIntent(), SCREEN_RECORD_REQUEST_CODE);
        return;
    }

    fab = (FloatingActionButton) findViewById(R.id.fab);

    if (isServiceRunning(RecorderService.class)) {
        Log.d(Const.TAG, "service is running");
        changeFabIcon(Status.RECORDING);
    }
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mMediaProjection == null && !isServiceRunning(RecorderService.class)) {
                //Request Screen recording permission
                startActivityForResult(mProjectionManager.createScreenCaptureIntent(),
                        SCREEN_RECORD_REQUEST_CODE);
            } else if (isServiceRunning(RecorderService.class)) {
                //stop recording if the service is already active and recording
                Intent stopRecording = new Intent(MainActivity.this, RecorderService.class);
                stopRecording.setAction(Const.SCREEN_RECORDING_STOP);
                startService(stopRecording);
                changeFabIcon(Status.STOPPED);
            }
        }
    });
    fab.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            //Show hint toast based on recording status
            if (isServiceRunning(RecorderService.class))
                Toast.makeText(MainActivity.this, R.string.fab_record_hint, Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(MainActivity.this, R.string.fab_stop_hint, Toast.LENGTH_SHORT).show();
            return true;
        }
    });

}