Skip to content

Instantly share code, notes, and snippets.

@alexjlockwood
Created May 24, 2012 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexjlockwood/2783675 to your computer and use it in GitHub Desktop.
Save alexjlockwood/2783675 to your computer and use it in GitHub Desktop.
newInstance() fragment instantiation #2
public class MyFragment {
/**
* Make the default constructor private to prevent direct
* instantiation. Now all instances of this class must be
* instantiated with a call to newInstance().
*/
private MyFragment() { }
/**
* Static factory method that takes an int parameter,
* initializes the fragment's arguments, and returns the
* new fragment to the client.
*/
public static MyFragment newInstance(int index) {
MyFragment f = new MyFragment();
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment