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.auth0.sample.ProfileActivity.java

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

    shares = getSharedPreferences("USER", 0);

    editor = shares.edit();/*from   w w w  . ja  v a 2s  .  c o m*/

    profile = getIntent().getParcelableExtra(Lock.AUTHENTICATION_ACTION_PROFILE_PARAMETER);

    access_token = getIntent().getParcelableExtra(Lock.AUTHENTICATION_ACTION_TOKEN_PARAMETER);
    Log.e("Access_Token", access_token.getAccessToken());
    editor.putString("ACCESS_TOKEN", access_token.getAccessToken());
    editor.commit();

    client = new AsyncHttpClient();
    app = (SampleApplication) getApplication();

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

    create = (TextView) toolbar.findViewById(R.id.create_tweet);
    create.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            createTweet(ProfileActivity.this);
        }
    });

    navigationView = (NavigationView) findViewById(R.id.navigation_view);

    header = navigationView.inflateHeaderView(R.layout.header);

    username = (TextView) header.findViewById(R.id.username);
    proPic = (ImageView) header.findViewById(R.id.profile_image);
    ((TextView) header.findViewById(R.id.email)).setText(profile.getEmail());

    /*
    client.get(((HashMap) profile.getExtraInfo().get("cover")).get("source").toString(), new FileAsyncHttpResponseHandler(ProfileActivity.this.getApplicationContext()) {
    @Override
    public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, Throwable throwable, File file) {
            
    }
            
    @Override
    public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, final File response) {
        ProfileActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Bitmap image = BitmapFactory.decodeFile(response.getPath());
            
            }
        });
            
            
    }
    });
    */
    username.setText(profile.getName());

    if (profile.getPictureURL() != null) {
        ImageLoader.getInstance().displayImage(profile.getPictureURL(), proPic);
    }

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        // This method will trigger on item Click of navigation menu
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {

            //Checking if the item is in checked state or not, if not make it in checked state

            nav = true;

            menuItem.setChecked(false);

            //Closing drawer on item click
            drawerLayout.closeDrawers();

            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            Fragment fragment;
            //Check to see which item was being clicked and perform appropriate action
            switch (menuItem.getItemId()) {
            case R.id.log_out:
                editor.remove("ENDPOINT_URL").remove("ACCESS_TOKEN").commit();
                Intent intent = getApplicationContext().getPackageManager()
                        .getLaunchIntentForPackage(getApplicationContext().getPackageName());
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);

            default:
                Toast.makeText(getApplicationContext(), "Somethings Wrong", Toast.LENGTH_SHORT).show();
                break;

            }
            return false;
        }
    });

    // Initializing Drawer Layout and ActionBarToggle
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
            (Toolbar) toolbar, R.string.openDrawer, R.string.closeDrawer) {

        @Override
        public void onDrawerClosed(View drawerView) {
            // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
            super.onDrawerClosed(drawerView);
            drawerLayout.setSelected(false);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank

            super.onDrawerOpened(drawerView);
        }
    };

    //Setting the actionbarToggle to drawer layout
    drawerLayout.setDrawerListener(actionBarDrawerToggle);

    //calling sync state is necessay or else your hamburger icon wont show up
    actionBarDrawerToggle.syncState();

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    final LinearLayoutManager layoutManager = new LinearLayoutManager(ProfileActivity.this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(layoutManager);

    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    // use this setting to improve performance if you know that changes
    // in content do not change the layout size of the RecyclerView
    mRecyclerView.setHasFixedSize(true);

    // use a linear layout manager
    mRecyclerView.setLayoutManager(layoutManager);

    items = new ArrayList<Tweet>();

    try {
        items.add(new Tweet(new JSONObject("{\n" + "      \"id\": \"f808d40b-4c54-4824-b3e0-8217e0840067\",\n"
                + "      \"type\": \"tweets\",\n" + "      \"attributes\": {\n"
                + "        \"author\": \"Tang Rufus\",\n" + "        \"body\": \"Hi all\",\n"
                + "        \"created-at\": \"2015-02-17T01:28:32.402Z\"\n" + "      }\n" + "    }")));
    } catch (JSONException e) {
        e.printStackTrace();
    }
    // specify an adapter (see also next example)
    mAdapter = new TweetAdapter(items);
    mRecyclerView.setAdapter(mAdapter);

    setEndpointURL(this);

    refresh_handler = new Handler();
    refresh_runnable = new Runnable() {
        @Override
        public void run() {
            if (!refresh) {
                ProfileActivity.this.get();
                refresh_handler.postDelayed(this, 50000);
                refresh = true;
            }
        }
    };

}

From source file:net.nakama.duckdroid.ui.Duckdroid.java

/**
 * //  w  w  w .j  av  a2 s.c o  m
 * @param result
 */
private void manageResult(ZeroClickResponse result) {

    if (result.isBang() && result.getRedirect() != null && !result.getRedirect().equals("")) {
        startBrowserIntent(result.getRedirect());

    } else if (result.getFlatResults().size() == 0) {

        TextView nt = new TextView(this);
        nt.setText("^_^ No result...");
        LinearLayout ll = (LinearLayout) findViewById(R.id.lt_main);
        ll.removeAllViews();
        ll.addView(nt);

    } else {

        FragmentManager manager = getFragmentManager();
        FragmentTransaction trx = manager.beginTransaction();
        ResultFragment resultFragment = new ResultFragment(result);
        trx.replace(R.id.lt_main, resultFragment);
        //trx.add(R.id.lt_main, resultFragment);
        trx.addToBackStack(null);
        trx.commit();
    }
}

From source file:com.slidinglayersample.MainActivity.java

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

    //        getPrefs();
    bindViews();/*from  w w w .j av a  2  s  .  c o m*/
    initState();

    FragmentManager fragmentManager = getFragmentManager();
    Fragment fragment = fragmentManager.findFragmentById(R.id.content_frame);

    if (fragment == null) {
        fragment = new MainFragment();

        fragmentManager.beginTransaction().add(R.id.content_frame, fragment).commit();
    }

    Button t1 = (Button) findViewById(R.id.txtProfile);
    t1.setText(User.getCurrentUser().getName() + "\n" + User.getCurrentUser().getEmail());

}

From source file:com.mastercard.masterpasswallet.fragments.MainCardFragment.java

private void setupViewpagerIndicator(int currentItem) {
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    Bundle data = new Bundle();
    data.putInt(ViewpagerIndicatorFragment.EXTRA_COUNT, mCards.size());
    data.putInt(ViewpagerIndicatorFragment.EXTRA_CURRENT_ITEM, currentItem);

    mViewpagerIndicator = new ViewpagerIndicatorFragment();
    mViewpagerIndicator.setArguments(data);

    ft.replace(R.id.lay_indicator_container, mViewpagerIndicator);
    ft.commit();/*  w w w .ja v  a2 s . c o m*/
}

From source file:com.orbar.pxdemo.MainActivity.java

private void selectItem(int position) {
    // update the main content by replacing fragments
    fragment = new FivePXBrowseImageListFragment();

    Bundle args = new Bundle();
    args.putInt(FivePXImageListFragment.ARG_CATEGORY,
            ((Category) mDrawerCategoryList.getItemAtPosition(position)).ordinal());

    fragment.setArguments(args);//from   ww w .j av  a2s.co m

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

    // update selected item and title, then close the drawer
    mDrawerCategoryList.setItemChecked(position, true);

    String selected = ((Category) mDrawerCategoryList.getItemAtPosition(position)).toString();
    setTitle(selected);

    mDrawerLayout.closeDrawer(mDrawerView);
}

From source file:com.orbar.pxdemo.MainActivity.java

protected void openMyAccount() {

    // remove selected item and update title, then close the drawer
    mDrawerCategoryList.clearChoices();/*www .  j ava2s  . c o m*/
    mDrawerCategoryList.requestLayout();

    setTitle(R.string.my_account);

    mDrawerLayout.closeDrawer(mDrawerView);

    fragment = new MyAccount2Fragment();

    Bundle args = new Bundle();

    args.putParcelable(MyAccount2Fragment.ARG_USER_BEAN, mFiveZeroZeroUserBean);
    args.putParcelable(MyAccount2Fragment.ARG_IMAGE_BEAN, mFiveZeroZeroImageBean);

    fragment.setArguments(args);

    FragmentManager fragmentManager = getFragmentManager();

    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).addToBackStack(null).commit();

}

From source file:com.polarion.starter.MainActivity.java

private void selectItem(int position) {
    // update the main content by replacing fragments
    mPreviousPosition = mCurrentPosition;
    mCurrentPosition = position;/* w  w w.  ja va2s . co  m*/
    DrawerItem drawerItem = mDrawerAdapter.getItem(position);
    if (mToolbar != null && drawerItem != null) {
        mToolbar.setTitle(drawerItem.title);
    }
    FragmentManager fragmentManager = getFragmentManager();
    Fragment fragment;
    Bundle args;
    switch (position) {
    case MenuChoices.HOME:
        fragment = new HomeFragment();

        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment)
                .addToBackStack(HomeFragment.class.getSimpleName()).commit();
        break;
    case MenuChoices.AGENDA:
        fragment = new AgendaItemFragment();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment)
                .addToBackStack(AgendaItemFragment.class.getSimpleName()).commit();
        break;
    case MenuChoices.SPEAKERS:
        fragment = new SpeakerItemFragment();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment)
                .addToBackStack(SpeakerItemFragment.class.getSimpleName()).commit();
        break;
    case MenuChoices.PARTNERS:
        fragment = new WebFragment();
        args = new Bundle();
        args.putString(WebFragment.ARG_URL_PARAM, "http://54.183.27.217/2015m/partners/index.html");
        args.putString(WebFragment.ARG_TITLE_PARAM, "Partners");
        fragment.setArguments(args);
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment)
                .addToBackStack(LocationFragment.class.getSimpleName()).commit();
        break;

    case MenuChoices.MAP:
        fragment = new LocationFragment();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment)
                .addToBackStack(LocationFragment.class.getSimpleName()).commit();
        break;
    case MenuChoices.RAFFLE:
        ParseUser currentUser = ParseUser.getCurrentUser();
        if (currentUser == null) { // if user is not logged in then force login before entering raffle
            fragment = new LoginFragment();
            args = new Bundle();
            args.putString(LoginFragment.FROM_ACTIVITY, RaffleFragment.class.getSimpleName());
            fragment.setArguments(args);
            setTitle("Log In");
            fragmentManager.beginTransaction().replace(R.id.content_frame, fragment)
                    .addToBackStack(LoginFragment.class.getSimpleName()).commit();
        } else {
            fragment = new RaffleFragment();
            fragmentManager.beginTransaction().replace(R.id.content_frame, fragment)
                    .addToBackStack(RaffleFragment.class.getSimpleName()).commit();
        }
        break;
    case MenuChoices.LOGIN:
        fragment = new LoginFragment();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment)
                .addToBackStack(LoginFragment.class.getSimpleName()).commit();
        break;

    case MenuChoices.SOCIAL:
        fragment = new SocialFragment();
        //        args = new Bundle();
        //        args.putInt(DrawerFragment.ARG_DRAWER_NUMBER, position);
        //        fragment.setArguments(args);
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
        break;

    //      default :
    //        fragment = new DrawerFragment();
    //        args = new Bundle();
    //        args.putInt(DrawerFragment.ARG_DRAWER_NUMBER, position);
    //        fragment.setArguments(args);
    //        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
    //        break;
    }
    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);

    if (drawerItem != null)
        setTitle(drawerItem.title);
    mDrawerLayout.closeDrawer(mDrawerList);
}

From source file:com.kennethmaffei.infinitegrid.HTTPCommManager.java

/**
 * Sets up a headless fragment that calls an AsyncTask to retrieve records in the db
 * /*from   www .j  ava 2s.c  o  m*/
 * @param activity - the activity, needed to access the fragment manager
 */
public void getDBData() {
    if (activity == null)
        return;

    synchronized (synchronizeObject) {
        if (!retrievalDone)
            return;

        retrievalDone = false;
        imageCount = 0;
        FragmentManager fm = activity.getFragmentManager();
        TaskFragment taskFragment = (TaskFragment) fm.findFragmentByTag(Constants.TAG_TASK_GETALLRECORDS);

        //If the Fragment is non-null, then it is currently being retained across a configuration change.         
        if (taskFragment == null) {
            taskFragment = new TaskFragment();
            Bundle args = new Bundle();
            args.putInt("type", CALLTYPE.ALL_RECORDS.ordinal());
            taskFragment.setArguments(args);
            fm.beginTransaction().add(taskFragment, Constants.TAG_TASK_GETALLRECORDS).commit();
        }
    }
}

From source file:com.ravi.apps.android.newsbytes.MainActivity.java

@Override
protected void onResume() {
    super.onResume();

    // Check if it's in two pane mode and preference has been changed.
    // If so, find and remove the details fragment.
    if (mIsTwoPaneMode && mHasPreferenceChanged) {
        // Get the fragment manager.
        FragmentManager fragmentManager = getFragmentManager();

        // Get the details fragment.
        DetailsFragment detailsFragment = (DetailsFragment) fragmentManager
                .findFragmentByTag(DetailsFragment.DETAILS_FRAGMENT_TAG);

        // Remove the details fragment.
        if (detailsFragment != null) {
            fragmentManager.beginTransaction().remove(detailsFragment).commit();
        }/* w w  w  .jav  a  2  s . c  o  m*/

        // Reset the preference changed flag.
        mHasPreferenceChanged = false;
    }
}

From source file:by.zatta.pilight.MainActivity.java

@Override
public void onLanguageListener(String language) {
    makeLocale(language);//from   w ww  .  j  a  v  a2  s.c  o  m
    invalidateOptionsMenu();
    initMenu();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    fm.popBackStack();
    ft.replace(R.id.fragment_main, new PrefFragment(), "prefs");
    ft.addToBackStack(null);
    ft.commit();
}