Last active
September 2, 2016 14:54
-
-
Save patrickmclaren/95a5617ebd3418acb0ca to your computer and use it in GitHub Desktop.
Asynchronous Fragments - https://patrickmclaren.com/asynchronously-managing-fragments/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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