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

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

Introduction

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

Prototype

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

Source Link

Document

Add a fragment to the activity state.

Usage

From source file:com.commonsware.android.feedfrags.AbstractFeedsActivity.java

protected void addItems(FragmentTransaction xaction, Feed feed) {
    ItemsFragment items = new ItemsFragment(true);

    items.setOnItemListener(this);
    items.loadUrl(feed.getUrl());/* ww w . j  ava  2  s.c om*/

    xaction.add(R.id.second_pane, items, "items");
}

From source file:com.codeslap.topy.BaseActivity.java

/**
 * Helper method that allows to initialize and add a fragment to activities that usually have
 * just one single fragment. Fragment is added using its class.getName() as tag.
 *
 * @param containerId   resource id of the fragment container (must be created through android resources)
 * @param fragmentClass the class of the fragment to setup
 * @param args          bundle with the arguments to pass to the fragment
 *///from   ww w.ja  v a  2s.  c o m
protected void setupBaseFragment(int containerId, Class<? extends Fragment> fragmentClass, Bundle args) {
    if (mSetContentViewAlreadyCalled) {
        View view = findViewById(containerId);
        if (!(view instanceof ViewGroup)) {
            throw new IllegalStateException(
                    "Since you already called setContentView, it must has a ViewGroup whose id is 'containerId'");
        }
    } else {
        FrameLayout container = new FrameLayout(this);
        container.setId(containerId);
        setContentView(container);
    }

    // let's check whether fragment is already added
    Fragment fragment = findFragment(fragmentClass);
    if (fragment == null) {
        // if not, let's create it and add it
        fragment = Fragment.instantiate(this, fragmentClass.getName(), args);

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(containerId, fragment, fragmentClass.getSimpleName());
        ft.commit();
    }
}

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

/**
 * This method controls both fragments, instructing them to display a
 * certain note./*w ww  .  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 = getSupportFragmentManager();
        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:com.kyakujin.android.tagnotepad.ui.NoteActivity.java

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

    setContentView(R.layout.activity_main);

    Intent intent = getIntent();//from  ww  w .j  a  v a  2 s  .  c om
    String action = intent.getAction();
    // DBG
    //  FragmentManager.enableDebugLogging(true);
    //  Log.d("debug", "ActivityTest: onCreate start"); // DBG

    // ?????????NoteEditFragment??????
    if (Intent.ACTION_SEND.equals(action)) {
        Bundle extras = intent.getExtras();

        if (extras != null) {
            String extBody = extras.getString(Intent.EXTRA_TEXT);
            String extTitle = extras.getString(Intent.EXTRA_SUBJECT);
            Bundle bundle = new Bundle();
            if (extBody != null) {
                bundle.putString(Config.SHARED_BODY, extBody);
            }
            if (extTitle != null) {
                bundle.putString(Config.SHARED_TITLE, extTitle);
            }

            bundle.putInt("action", Config.ACTION_SENDED);
            Fragment fg = NoteEditFragment.newInstance();
            FragmentManager manager = getSupportFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            fg.setArguments(bundle);
            transaction.add(android.R.id.content, fg, Config.TAG_NOTEEDIT_FRAGM);
            transaction.commit();
        }
    } else {
        Fragment fg = NoteListFragment.newInstance();
        if (!fg.isAdded()) {
            // NoteListFragment?????
            FragmentManager fm = getSupportFragmentManager();
            for (int i = 0; i < fm.getBackStackEntryCount(); i++) {
                fm.popBackStack();
            }

            getSupportFragmentManager().beginTransaction()
                    .replace(android.R.id.content, fg, Config.TAG_NOTELIST_FRAGM).commit();
        }
    }

    handleIntent(getIntent());
}

From source file:com.aniruddhc.acemusic.player.NowPlayingQueueActivity.NowPlayingQueueActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    mContext = this;
    sharedPreferences = getSharedPreferences("com.aniruddhc.acemusic.player", Context.MODE_PRIVATE);

    //Get the screen's parameters.
    DisplayMetrics displayMetrics = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int screenWidth = displayMetrics.widthPixels;

    //Set the UI theme.
    if (sharedPreferences.getString(Common.CURRENT_THEME, "LIGHT_CARDS_THEME").equals("DARK_THEME")
            || sharedPreferences.getString(Common.CURRENT_THEME, "LIGHT_CARDS_THEME")
                    .equals("DARK_CARDS_THEME")) {
        setTheme(R.style.AppTheme);//w ww .j a v a  2  s  .  c  o m
    } else {
        setTheme(R.style.AppThemeLight);
    }

    super.onCreate(savedInstanceState);

    if (getOrientation().equals("PORTRAIT")) {

        //Finish this activity and relaunch the activity that called this one.
        Intent intent = new Intent(this, (Class<?>) getIntent().getSerializableExtra("CALLING_CLASS"));
        intent.putExtras(getIntent());
        intent.putExtra("NEW_PLAYLIST", false);
        intent.putExtra("CALLED_FROM_FOOTER", true);
        intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        finish();
        startActivity(intent);

        return;

    } else {

        setContentView(R.layout.activity_now_playing_queue);

        final Fragment nowPlayingQueueFragment = new NowPlayingQueueFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.now_playing_queue_container, nowPlayingQueueFragment, "nowPlayingQueueFragment");
        transaction.commit();

        SpannableString s = new SpannableString(getResources().getString(R.string.current_queue));
        s.setSpan(new TypefaceSpan(this, "RobotoCondensed-Light"), 0, s.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        // Update the action bar title with the TypefaceSpan instance.
        ActionBar actionBar = getActionBar();
        actionBar.setTitle(s);
        actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.holo_gray_selector));

    }

}

From source file:com.Duo.music.player.NowPlayingQueueActivity.NowPlayingQueueActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    mContext = this;
    sharedPreferences = getSharedPreferences("com.jams.music.player", Context.MODE_PRIVATE);

    //Get the screen's parameters.
    DisplayMetrics displayMetrics = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int screenWidth = displayMetrics.widthPixels;

    //Set the UI theme.
    if (sharedPreferences.getString(Common.CURRENT_THEME, "LIGHT_CARDS_THEME").equals("DARK_THEME")
            || sharedPreferences.getString(Common.CURRENT_THEME, "LIGHT_CARDS_THEME")
                    .equals("DARK_CARDS_THEME")) {
        setTheme(R.style.AppTheme);//from   w  ww  .  ja v a2  s  . com
    } else {
        setTheme(R.style.AppThemeLight);
    }

    super.onCreate(savedInstanceState);

    if (getOrientation().equals("PORTRAIT")) {

        //Finish this activity and relaunch the activity that called this one.
        Intent intent = new Intent(this, (Class<?>) getIntent().getSerializableExtra("CALLING_CLASS"));
        intent.putExtras(getIntent());
        intent.putExtra("NEW_PLAYLIST", false);
        intent.putExtra("CALLED_FROM_FOOTER", true);
        intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        finish();
        startActivity(intent);

        return;

    } else {

        setContentView(R.layout.activity_now_playing_queue);

        final Fragment nowPlayingQueueFragment = new NowPlayingQueueFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.now_playing_queue_container, nowPlayingQueueFragment, "nowPlayingQueueFragment");
        transaction.commit();

        SpannableString s = new SpannableString(getResources().getString(R.string.current_queue));
        s.setSpan(new TypefaceSpan(this, "RobotoCondensed-Light"), 0, s.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        // Update the action bar title with the TypefaceSpan instance.
        ActionBar actionBar = getActionBar();
        actionBar.setTitle(s);
        actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.holo_gray_selector));

    }

}

From source file:com.jelly.music.player.NowPlayingQueueActivity.NowPlayingQueueActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    mContext = this;
    sharedPreferences = getSharedPreferences("com.jelly.music.player", Context.MODE_PRIVATE);

    //Get the screen's parameters.
    DisplayMetrics displayMetrics = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int screenWidth = displayMetrics.widthPixels;

    //Set the UI theme.
    if (sharedPreferences.getString(Common.CURRENT_THEME, "LIGHT_CARDS_THEME").equals("DARK_THEME")
            || sharedPreferences.getString(Common.CURRENT_THEME, "LIGHT_CARDS_THEME")
                    .equals("DARK_CARDS_THEME")) {
        setTheme(R.style.AppTheme);/*from   w w w.  j a  va 2  s  .c om*/
    } else {
        setTheme(R.style.AppThemeLight);
    }

    super.onCreate(savedInstanceState);

    if (getOrientation().equals("PORTRAIT")) {

        //Finish this activity and relaunch the activity that called this one.
        Intent intent = new Intent(this, (Class<?>) getIntent().getSerializableExtra("CALLING_CLASS"));
        intent.putExtras(getIntent());
        intent.putExtra("NEW_PLAYLIST", false);
        intent.putExtra("CALLED_FROM_FOOTER", true);
        intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        finish();
        startActivity(intent);

        return;

    } else {

        setContentView(R.layout.activity_now_playing_queue);

        final Fragment nowPlayingQueueFragment = new NowPlayingQueueFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.now_playing_queue_container, nowPlayingQueueFragment, "nowPlayingQueueFragment");
        transaction.commit();

        SpannableString s = new SpannableString(getResources().getString(R.string.current_queue));
        s.setSpan(new TypefaceSpan(this, "RobotoCondensed-Light"), 0, s.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        // Update the action bar title with the TypefaceSpan instance.
        ActionBar actionBar = getActionBar();
        actionBar.setTitle(s);
        actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.holo_gray_selector));

    }

}

From source file:com.hippo.nimingban.ui.PostActivity.java

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

    setStatusBarColor(ResourcesUtils.getAttrColor(this, R.attr.colorPrimaryDark));
    setContentView(R.layout.activity_post);

    mPostLayout = (PostLayout) findViewById(R.id.fragment_container);

    if (mPostLayout != null) {
        if (savedInstanceState == null) {
            PostFragment postFragment = new PostFragment();
            postFragment.setArguments(createArgs());
            postFragment.setFragmentHost(this);
            postFragment.setCallback(this);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.fragment_container, postFragment, TAG_FRAGMENT_POST);
            transaction.commitAllowingStateLoss();
        } else {// w  w  w .j  a  v  a2 s  .  co m
            FragmentManager fragmentManager = getSupportFragmentManager();
            PostFragment postFragment = (PostFragment) fragmentManager.findFragmentByTag(TAG_FRAGMENT_POST);
            if (postFragment != null) {
                postFragment.setFragmentHost(this);
                postFragment.setCallback(this);
            }
            TypeSendFragment typeSendFragment = (TypeSendFragment) fragmentManager
                    .findFragmentByTag(TAG_FRAGMENT_TYPE_SEND);
            if (typeSendFragment != null) {
                typeSendFragment.setFragmentHost(this);
                typeSendFragment.setCallback(this);
            }
        }
    }
}

From source file:com.example.hackinghealthclinification.app.ClinificationApp.java

private void initializeLaunchButtons() {
    mViewAppointmentButton = (CustomButtonView) findViewById(R.id.launch_button_view_appointments);
    mViewAppointmentButton.setOnClickListener(new View.OnClickListener() {
        @Override/*from w w  w  .  j  a va 2s  .c  o m*/
        public void onClick(View v) {
            AppointmentListFragment appointmentListFragment = new AppointmentListFragment();
            android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager()
                    .beginTransaction();
            transaction.add(android.R.id.content, appointmentListFragment, AppointmentListFragment.TAG);
            transaction.addToBackStack(AppointmentListFragment.TAG);
            transaction.commit();
        }
    });

    mViewAppointmentButton = (CustomButtonView) findViewById(R.id.launch_button_test);
    mViewAppointmentButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
}

From source file:com.enadein.carlogbook.core.AppMediator.java

public void addFragment(int containterViewId, BaseFragment fragment, String tag, boolean backStack,
        String backStackName) {/*from  w  w w  . j av  a  2 s . co  m*/
    FragmentTransaction transaction = fragmentManager.beginTransaction();

    setUpAnimation(transaction);

    if (tag != null) {
        transaction.add(containterViewId, fragment, tag);
    } else {
        transaction.add(containterViewId, fragment);
    }

    if (backStack) {
        transaction.addToBackStack(backStackName);
    }

    transaction.commit();
    ActivityCompat.invalidateOptionsMenu(activity);
    activity.supportInvalidateOptionsMenu();
}