Skip to content

Instantly share code, notes, and snippets.

@patrickmclaren
Last active September 2, 2016 14:54
Show Gist options
  • Select an option

  • Save patrickmclaren/95a5617ebd3418acb0ca to your computer and use it in GitHub Desktop.

Select an option

Save patrickmclaren/95a5617ebd3418acb0ca to your computer and use it in GitHub Desktop.

Revisions

  1. patrickmclaren revised this gist Sep 2, 2016. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion FragmentActivity.java
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,13 @@
    public FragmentActivity extends Activity {
    package com.patrickmclaren.examples.asyncfragments;

    import android.app.Activity;
    import android.app.FragmentManager;
    import android.app.FragmentTransaction;

    import android.content.res.Configuration;
    import android.os.Bundle;

    public class FragmentActivity extends Activity {
    private ExampleFragment mFragment;

    @Override
  2. patrickmclaren created this gist Mar 19, 2015.
    32 changes: 32 additions & 0 deletions FragmentActivity.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    public FragmentActivity extends Activity {
    private ExampleFragment mFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super(savedInstanceState)
    }

    public void addFragment() {
    FragmentManager fm = getSupportFragmentManager();

    mFragment = (ExampleFragment) fm.findFragmentByTag(ExampleFragment.TAG);
    if (mFragment == null) {
    FragmentTransaction ft = fm.beginTransaction();
    mFragment = new ExampleFragment();

    ft.add(R.id.container, mFragment, ExampleFragment.TAG);

    try {
    ft.commit();
    } catch(IllegalStateException e) {
    // Activity may be destroyed, you can't complete this transaction
    mFragment = null;
    }
    } else {
    // Fragment already added
    if (mFragment.isInLayout()) {
    // Fragment in View hierarchy
    }
    }
    }
    }