Java Code Examples for android.app.FragmentTransaction#add()

The following examples show how to use android.app.FragmentTransaction#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RssfeedActivity.java    From codeexamples-android with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_rssfeed);
	
	mHasOnePane = findViewById(R.id.container) != null;
	
	if (mHasOnePane) {
		FragmentManager fm = getFragmentManager();
		if (fm.findFragmentByTag("list") == null) {
			// add list fragment
			FragmentTransaction trx = fm.beginTransaction();
			trx.add(R.id.container, new MyListFragment(), "list");
			trx.commit();
		}
	} // else, layout handles it

	if (savedInstanceState != null) {
		mLastSelectedLink = savedInstanceState.getString("selectedLink", null);
		onRssItemSelected(mLastSelectedLink);
	}
	
}
 
Example 2
Source File: MainActivity.java    From CSCI4669-Fall15-Android with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState)
{
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   // return if Activity is being restored, no need to recreate GUI
   if (savedInstanceState != null) 
      return;

   // check whether layout contains fragmentContainer (phone layout);
   // ContactListFragment is always displayed
   if (findViewById(R.id.fragmentContainer) != null) 
   {
      // create ContactListFragment
      contactListFragment = new ContactListFragment();
      
      // add the fragment to the FrameLayout
      FragmentTransaction transaction = 
         getFragmentManager().beginTransaction();
      transaction.add(R.id.fragmentContainer, contactListFragment);
      transaction.commit(); // causes ContactListFragment to display
   }
}
 
Example 3
Source File: MainActivity.java    From codeexamples-android with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
	FragmentManager manager = getFragmentManager();
	switch (item.getItemId()) {
	case R.id.actionadd1:
		FragmentTransaction transaction = manager.beginTransaction();
		transaction.add(R.id.fragmentcontainer1, new MyListFragment());
		transaction
				.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
		transaction.commit();
		break;
	case R.id.actionadd2:
		transaction = manager.beginTransaction();
		transaction.add(R.id.fragmentcontainer2, new DetailFragment());
		transaction.commit();
		break;

	default:
		break;
	}

	// transaction = getFragmentManager().beginTransaction();
	// transaction.add(R.id.fragmentcontainer2, new DetailFragment());
	// transaction.commit();
	return true;
}
 
Example 4
Source File: MainActivity.java    From CSCI4669-Fall15-Android with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState)
{
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   // return if Activity is being restored, no need to recreate GUI
   if (savedInstanceState != null) 
      return;

   // check whether layout contains fragmentContainer (phone layout);
   // ContactListFragment is always displayed
   if (findViewById(R.id.fragmentContainer) != null) 
   {
      // create ContactListFragment
      contactListFragment = new ContactListFragment();
      
      // add the fragment to the FrameLayout
      FragmentTransaction transaction = 
         getFragmentManager().beginTransaction();
      transaction.add(R.id.fragmentContainer, contactListFragment);
      transaction.commit(); // causes ContactListFragment to display
   }
}
 
Example 5
Source File: SettingsActivity.java    From loaned-android with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	// If the available screen size is that of an average tablet (as defined
	// in the Android documentation) then allow the screen to rotate
	if(getResources().getBoolean(R.bool.lock_orientation)){
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}
	getActionBar().setDisplayHomeAsUpEnabled(true);
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_settings);

	PreferenceFragment mPrefsFrag = new SettingsFragment();

	FragmentTransaction mFragMan = getFragmentManager().beginTransaction();
	mFragMan.add(R.id.settings_frame, mPrefsFrag);
	mFragMan.commit();
}
 
Example 6
Source File: MemberListActivity.java    From tapchat-android with Apache License 2.0 6 votes vote down vote up
@Subscribe public void onServiceStateChanged(ServiceStateChangedEvent event) {
    TapchatService service = event.getService();
    if (service.getConnectionState() != TapchatService.STATE_LOADED) {
        return;
    }

    long connectionId = getIntent().getLongExtra(BufferFragment.ARG_CONNECTION_ID, -1);
    long bufferId = getIntent().getLongExtra(BufferFragment.ARG_BUFFER_ID, -1);

    mChannel = (ChannelBuffer) service.getConnection(connectionId).getBuffer(bufferId);

    setTitle(getString(R.string.members_title_format, mChannel.getDisplayName()));

    Bundle args = new Bundle();
    args.putLong(BufferFragment.ARG_CONNECTION_ID, mChannel.getConnection().getId());
    args.putLong(BufferFragment.ARG_BUFFER_ID, mChannel.getId());

    MemberListFragment fragment = (MemberListFragment) getFragmentManager().findFragmentByTag("members");
    if (fragment == null) {
        fragment = new MemberListFragment();
        fragment.setArguments(args);
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.add(R.id.content, fragment, "members");
        transaction.commit();
    }
}
 
Example 7
Source File: QuoteViewerActivity.java    From coursera-android with MIT License 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	// Get the string arrays with the titles and qutoes
	TitleArray = getResources().getStringArray(R.array.Titles);
	QuoteArray = getResources().getStringArray(R.array.Quotes);
	
	setContentView(R.layout.main);

	// Get a reference to the FragmentManager
	mFragmentManager = getFragmentManager();

	// Start a new FragmentTransaction
	FragmentTransaction fragmentTransaction = mFragmentManager
			.beginTransaction();

	// Add the TitleFragment to the layout
	fragmentTransaction.add(R.id.title_fragment_container, mTitlesFragment);
	
	// Commit the FragmentTransaction
	fragmentTransaction.commit();

}
 
Example 8
Source File: MapsAppActivity.java    From maps-app-android with Apache License 2.0 6 votes vote down vote up
/**
 * Opens the content browser that shows the user's maps.
 */
private void showContentBrowser() {
	FragmentManager fragmentManager = getFragmentManager();
	Fragment browseFragment = fragmentManager.findFragmentByTag(ContentBrowserFragment.TAG);
	if (browseFragment == null) {
		browseFragment = new ContentBrowserFragment();
	}

	if (!browseFragment.isVisible()) {
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		transaction.add(R.id.maps_app_activity_content_frame, browseFragment, ContentBrowserFragment.TAG);
		transaction.addToBackStack(null);
		transaction.commit();

		invalidateOptionsMenu(); // reload the options menu
	}

	mDrawerLayout.closeDrawers();
}
 
Example 9
Source File: IntroductionActivity.java    From io2014-codelabs with Apache License 2.0 5 votes vote down vote up
private void goToFragment(String oldFragmentTag, String newFragmentTag, Fragment newFragment) {
    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.intro_frame, newFragment, newFragmentTag);
    Fragment fragment = getFragmentManager().findFragmentByTag(oldFragmentTag);
    if (fragment != null) {
        fragmentTransaction.remove(getFragmentManager().findFragmentByTag(oldFragmentTag));
    }
    fragmentTransaction.commit();
}
 
Example 10
Source File: BaseFragmentUtil.java    From android-commons with Apache License 2.0 5 votes vote down vote up
public static void replaceFragmentToLayout(final int containerId, final FragmentManager fragmentManager,
                                           final Fragment fragment, final String tag) {
  final FragmentTransaction ft = fragmentManager.beginTransaction();
  final Fragment previousFragment = fragmentManager
    .findFragmentByTag(tag);
  if (previousFragment != null) {
    ft.remove(previousFragment);
  }
  ft.add(containerId, fragment, tag);
  ft.commit();
}
 
Example 11
Source File: ActivityUtils.java    From RichEditor with Apache License 2.0 5 votes vote down vote up
/**
 * The {@code fragment} is added to the container view with id {@code frameId}. The operation is
 * performed by the {@code fragmentManager}.
 *
 */
@SuppressLint("RestrictedApi")
public static void addFragmentToActivity (@NonNull FragmentManager fragmentManager,
                                          @NonNull Fragment fragment, int frameId) {
    checkNotNull(fragmentManager);
    checkNotNull(fragment);
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.add(frameId, fragment);
    transaction.commit();
}
 
Example 12
Source File: QuoteViewerActivity.java    From coursera-android with MIT License 5 votes vote down vote up
@Override
public void onListSelection(int index) {

	// If the QuoteFragment has not been added, add it now
	if (!mQuoteFragment.isAdded()) {
	
		// Start a new FragmentTransaction
		FragmentTransaction fragmentTransaction = mFragmentManager
				.beginTransaction();

		// Add the QuoteFragment to the layout
		fragmentTransaction.add(R.id.quote_fragment_container,
				mQuoteFragment);

		// Add this FragmentTransaction to the backstack
		fragmentTransaction.addToBackStack(null);
		
		// Commit the FragmentTransaction
		fragmentTransaction.commit();
		
		// Force Android to execute the committed FragmentTransaction
		mFragmentManager.executePendingTransactions();
	}
	
	if (mQuoteFragment.getShownIndex() != index) {

		// Tell the QuoteFragment to show the quote string at position index
		mQuoteFragment.showQuoteAtIndex(index);
	
	}
}
 
Example 13
Source File: BaseActivity.java    From Companion-For-PUBG-Android with MIT License 5 votes vote down vote up
public void showFragment(@NonNull final Fragment fragment,
                         @IdRes final int contentView,
                         @NonNull final String tag) {
    final FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();
    final Fragment topFragment = getTopFragment();
    if (topFragment == null) {
        fragTransaction.add(contentView, fragment, tag);
    } else {
        fragTransaction.replace(contentView, fragment, tag);
    }
    fragTransaction.commit();
    getFragmentManager().executePendingTransactions();
}
 
Example 14
Source File: SampleFragmentActivity.java    From let with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fragment);

    FragmentTransaction ft = getFragmentManager().beginTransaction();

    ft.add(R.id.frag_container, new SampleFragment());
    ft.commit();
}
 
Example 15
Source File: MainActivity.java    From codeexamples-android with Eclipse Public License 1.0 5 votes vote down vote up
public void onTabSelected(Tab tab, FragmentTransaction ft) {
	// Check if the fragment is already initialized
	if (mFragment == null) {
		// If not, instantiate and add it to the activity
		mFragment = Fragment.instantiate(mActivity, mClass.getName());
		ft.add(android.R.id.content, mFragment, mTag);
	} else {
		// If it exists, simply attach it in order to show it
		ft.setCustomAnimations(android.R.animator.fade_in,
				R.animator.animationtest);
		ft.attach(mFragment);
	}
}
 
Example 16
Source File: SettingAboutActivity.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
protected void onCreate(Bundle bundle)
{
    super.onCreate(bundle);
    FragmentTransaction fragmenttransaction = getFragmentManager().beginTransaction();
    b = (SettingAboutFragment)Fragment.instantiate(this, cn/com/smartdevices/bracelet/ui/SettingAboutActivity$SettingAboutFragment.getName());
    fragmenttransaction.add(0x1020002, b);
    fragmenttransaction.commit();
    a();
}
 
Example 17
Source File: BaseFragmentUtil.java    From android-commons with Apache License 2.0 5 votes vote down vote up
public static void showDialogFragment(final DialogFragment dialog,
                                      final String tag, final FragmentManager fragmentManager) {
  final FragmentTransaction ft = fragmentManager.beginTransaction();
  final Fragment prev = fragmentManager.findFragmentByTag(tag);
  if (prev != null) {
    ft.remove(prev);
  }
  ft.add(dialog, tag);
  ft.commitAllowingStateLoss();
}
 
Example 18
Source File: CertificateManagementActivity.java    From moVirt with Apache License 2.0 5 votes vote down vote up
@Override
public void showDownloadCustomCaDialog(String url, boolean startNewChain) {
    if (getFragmentManager().findFragmentByTag(DOWNLOAD_CA_ISSUER) == null) { // updateViews can call this multiple times
        DownloadCustomCertDialogFragment dialog = new DownloadCustomCertDialogFragment_();
        dialog.setUrl(url);
        dialog.setStartNewChain(startNewChain);
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.add(dialog, DOWNLOAD_CA_ISSUER);
        transaction.commitAllowingStateLoss();
    }
}
 
Example 19
Source File: VideoExampleActivity.java    From tv-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video_example);

    if (savedInstanceState == null) {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(R.id.videoFragment, new VideoConsumptionExampleFragment(),
                VideoConsumptionExampleFragment.TAG);
        ft.commit();
    }
}
 
Example 20
Source File: PreferenceActivity.java    From nono-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_preference,"NONo个性化");
    FragmentManager fragmentManager = getFragmentManager();
             FragmentTransaction transaction = fragmentManager.beginTransaction();
             NONoPreferenceFragment prefFragment = new NONoPreferenceFragment();
             transaction.add(R.id.prefFragment, prefFragment);
             transaction.commit();
}