Example usage for android.support.v4.app FragmentTransaction replace

List of usage examples for android.support.v4.app FragmentTransaction replace

Introduction

In this page you can find the example usage for android.support.v4.app FragmentTransaction replace.

Prototype

public abstract FragmentTransaction replace(int containerViewId, Fragment fragment);

Source Link

Document

Calls #replace(int,Fragment,String) with a null tag.

Usage

From source file:com.example.krishna.letsshield.MainActivity.java

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

    // Set the content of the activity to use the activity_main.xml layout file
    setContentView(R.layout.activity_main);
    fragment = FirstFragment.newInstance();

    //Manually displaying the first fragment - one time only
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.frame_layout, fragment);
    transaction.commit();/*ww w.j a  va2 s .co  m*/

    // Set default username is anonymous.
    mUsername = ANONYMOUS;
    mFirebaseAuth = FirebaseAuth.getInstance();
    mFirebaseUser = mFirebaseAuth.getCurrentUser();
    if (mFirebaseUser == null) {
        // Not signed in, launch the Sign In activity
        startActivity(new Intent(this, SignInActivity.class));
        finish();
        return;
    } else {
        mUsername = mFirebaseUser.getDisplayName();
        if (mFirebaseUser.getPhotoUrl() != null) {
            mPhotoUrl = mFirebaseUser.getPhotoUrl().toString();
        }
    }

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API).build();

    // Initialize ProgressBar and RecyclerView.

    /*  findViewById(R.id.fab_new_post).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        startActivity(new Intent(MainActivity.this, NewPostActivity2.class));
    }
      });*/

    bottomBar = (BottomBar) findViewById(R.id.bottomBar);
    bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
        @Override
        public void onTabSelected(@IdRes int tabId) {
            switch (tabId) {
            case R.id.home_tabs:
                selectedFragment = FirstFragment.newInstance();
                break;
            case R.id.my_orders:
                selectedFragment = SecondFragment.newInstance();
                break;
            case R.id.location_history:
                selectedFragment = ThirdFragment.newInstance();
                break;

            }
            switchFragment(selectedFragment);
        }
    });

}

From source file:com.denizensoft.appcompatlib.AppActivity.java

public void invokeFragment(int nFrameContainer, Fragment fragment) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

    transaction.replace(nFrameContainer, fragment);

    transaction.commit();//from  ww w. j a  v  a2s.  com
}

From source file:com.example.mego.adas.videos.ui.LiveStreamingFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_live_streaming, container, false);

    initializeScreen(rootView);/* w ww.java  2s  .  c  o  m*/

    //check the internet connection
    ConnectivityManager connectivityManager = (ConnectivityManager) getActivity()
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.isConnected()) {

        //show a loading bar till the onDataChange triggered
        loadingBar.setVisibility(View.VISIBLE);
        //set up the firebase
        mFirebaseDatabase = FirebaseDatabase.getInstance();

        //get the current user uid
        User currentUser = AuthenticationUtilities.getCurrentUser(getContext());
        String uid = currentUser.getUserUid();

        //get the references for the child
        //the  child for the  live video id
        mLiveVideoIdDatabaseReference = mFirebaseDatabase.getReference().child(Constants.FIREBASE_USERS)
                .child(uid).child(Constants.FIREBASE_USER_INFO)
                .child(Constants.FIREBASE_LIVE_STREAMING_VIDEO_ID);

        //set the id for listener
        mLiveVideoIdValueListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                //hide the loading bar
                loadingBar.setVisibility(View.INVISIBLE);

                if (dataSnapshot.exists()) {
                    //get the current video id
                    currentLiveVideoId = dataSnapshot
                            .getValue(Constants.FIREBASE_LIVE_STREAMING_VIDEO_ID.getClass());
                    if (currentLiveVideoId.equals(Constants.LIVE_STREAMING_NO_LIVE_VIDEO)) {

                        //show the warning image and text
                        noLiveVideoTextView.setVisibility(View.VISIBLE);
                        liveStreamingImageView.setVisibility(View.VISIBLE);

                        //hide the fragment
                        liveVideoContainer.setVisibility(View.GONE);

                    } else {

                        //hide the warning image and text
                        noLiveVideoTextView.setVisibility(View.GONE);
                        liveStreamingImageView.setVisibility(View.GONE);

                        liveVideoContainer.setVisibility(View.VISIBLE);

                        //setup the player fragment
                        YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment
                                .newInstance();

                        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
                        transaction.replace(R.id.watch_live_video_player, youTubePlayerFragment).commit();

                        youTubePlayerFragment.initialize(Constants.API_KEY,
                                new YouTubePlayer.OnInitializedListener() {
                                    @Override
                                    public void onInitializationSuccess(YouTubePlayer.Provider provider,
                                            YouTubePlayer youTubePlayer, boolean wasRestored) {

                                        //load and play the video with current id
                                        youTubePlayer.loadVideo(currentLiveVideoId);
                                        youTubePlayer.play();
                                    }

                                    @Override
                                    public void onInitializationFailure(YouTubePlayer.Provider provider,
                                            YouTubeInitializationResult youTubeInitializationResult) {
                                        Toast.makeText(getContext(), R.string.can_not_initialize_youtube_player,
                                                Toast.LENGTH_LONG).show();

                                    }
                                });

                    }
                } else {
                    mLiveVideoIdDatabaseReference.setValue(Constants.NO_LIVE_VIDEO);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        };

        //attach the listener
        mLiveVideoIdDatabaseReference.addValueEventListener(mLiveVideoIdValueListener);
    } else {
        noLiveVideoTextView.setVisibility(View.VISIBLE);
        liveStreamingImageView.setVisibility(View.VISIBLE);
    }

    // Inflate the layout for this fragment
    return rootView;
}

From source file:com.example.android.amplacenta.MainActivity.java

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

    if (savedInstanceState == null) {
        Intent intent = getIntent();//from   w w  w. ja  v  a2s  . com
        boolean isMaster = intent.getBooleanExtra(SelectLayoutActivity.LAYOUT_TYPE, false);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        Fragment fragment;
        if (isMaster) {
            fragment = new HostFragment();
        } else {
            fragment = new GuestFragment();
        }
        transaction.replace(R.id.sample_content_fragment, fragment);
        transaction.commit();
    }
}

From source file:com.battlelancer.seriesguide.ui.OverviewActivity.java

private void setupPanes() {
    Fragment showsFragment = ShowFragment.newInstance(mShowId);
    FragmentTransaction ft1 = getSupportFragmentManager().beginTransaction();
    ft1.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
    ft1.replace(R.id.fragment_show, showsFragment);
    ft1.commit();//from   ww w .  j av  a2s . c o  m

    Fragment overviewFragment = OverviewFragment.newInstance(mShowId);
    FragmentTransaction ft2 = getSupportFragmentManager().beginTransaction();
    ft2.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
    ft2.replace(R.id.fragment_overview, overviewFragment);
    ft2.commit();

    Fragment seasonsFragment = SeasonsFragment.newInstance(mShowId);
    FragmentTransaction ft3 = getSupportFragmentManager().beginTransaction();
    ft3.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
    ft3.replace(R.id.fragment_seasons, seasonsFragment);
    ft3.commit();
}

From source file:com.ervinwang.fragments.MainActivity.java

public void onArticleSelected(int position) {
    // The user selected the headline of an article from the
    // HeadlinesFragment

    // Capture the article fragment from the activity layout
    ArticleFragment articleFrag = (ArticleFragment) getSupportFragmentManager()
            .findFragmentById(R.id.article_fragment);

    if (articleFrag != null) {
        // If article frag is available, we're in two-pane layout...

        // Call a method in the ArticleFragment to update its content
        articleFrag.updateArticleView(position);

    } else {/* w w w.j a v a 2s . c o  m*/
        // If the frag is not available, we're in the one-pane layout and
        // must swap frags...

        // Create fragment and give it an argument for the selected article
        ArticleFragment newFragment = new ArticleFragment();
        Bundle args = new Bundle();
        args.putInt(ArticleFragment.ARG_POSITION, position);
        newFragment.setArguments(args);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this
        // fragment,
        // and add the transaction to the back stack so the user can
        // navigate back
        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);

        // Commit the transaction
        transaction.commit();
    }
}

From source file:com.bruce.study.demo.studydata.fragment.FragmentsActivity.java

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    LogUtils.d(String.format(" %s ?Demo %s", position + 1, fragmentNamesList.get(position)));
    LogUtils.i("? -->" + Thread.currentThread());
    fragmentTransaction.replace(R.id.rl_container, fragments.get(position));
    fragmentTransaction.addToBackStack(fragmentNamesList.get(position));
    fragmentTransaction.commit();//from   w  w  w. j  a  v a2s. c  o  m
    if (!rl_container.isShown()) {
        rl_container.setVisibility(View.VISIBLE);
    }
    if (lv_demo_list.isShown()) {
        lv_demo_list.setVisibility(View.GONE);
    }
}

From source file:com.android.fastinfusion.ui.activity.MyActivity.java

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    switch (position) {
    case 0:/* www . j av a 2s  .co m*/
        if (fragmentPage1 == null) {
            fragmentPage1 = new FragmentPage1();
        }
        fragmentTransaction.replace(R.id.container, fragmentPage1);
        break;
    case 1:
        if (fragmentPage2 == null) {
            fragmentPage2 = new FragmentPage2();
        }
        fragmentTransaction.replace(R.id.container, fragmentPage2);
        break;
    default:
        break;
    }
}

From source file:com.example.diego.activitytracker.MainActivity.java

@Override
public void okListener() {
    HeadlinesFragment newFragment = new HeadlinesFragment();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

    // Replace whatever is in the fragment_container view with this fragment,
    // and add the transaction to the back stack so the user can navigate back
    transaction.replace(R.id.fragment_container, newFragment);
    transaction.addToBackStack(null);/*w ww .  java2  s  .c  o m*/

    // Commit the transaction
    transaction.commit();
}

From source file:com.example.diego.activitytracker.MainActivity.java

@Override
public void addNewArtical() {
    AddArticleFragment newFragment = new AddArticleFragment();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

    // Replace whatever is in the fragment_container view with this fragment,
    // and add the transaction to the back stack so the user can navigate back
    transaction.replace(R.id.fragment_container, newFragment);
    transaction.addToBackStack(null);//from   www .ja v  a  2 s  . c om

    // Commit the transaction
    transaction.commit();

}