Skip to content

Instantly share code, notes, and snippets.

@patrickmclaren
Last active September 2, 2016 14:54
Show Gist options
  • Save patrickmclaren/95a5617ebd3418acb0ca to your computer and use it in GitHub Desktop.
Save patrickmclaren/95a5617ebd3418acb0ca to your computer and use it in GitHub Desktop.
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
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
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment