Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes they do.</p> <p>You shouldn't really be overriding the constructor anyway. You should have a <code>newInstance()</code> static method defined and pass any parameters via arguments (bundle)</p> <p>For example:</p> <pre><code>public static final MyFragment newInstance(int title, String message) { MyFragment f = new MyFragment(); Bundle bdl = new Bundle(2); bdl.putInt(EXTRA_TITLE, title); bdl.putString(EXTRA_MESSAGE, message); f.setArguments(bdl); return f; } </code></pre> <p>And of course grabbing the args this way:</p> <pre><code>@Override public void onCreate(Bundle savedInstanceState) { title = getArguments().getInt(EXTRA_TITLE); message = getArguments().getString(EXTRA_MESSAGE); //... //etc //... } </code></pre> <p>Then you would instantiate from your fragment manager like so:</p> <pre><code>@Override public void onCreate(Bundle savedInstanceState) { if (savedInstanceState == null){ getSupportFragmentManager() .beginTransaction() .replace(R.id.content, MyFragment.newInstance( R.string.alert_title, "Oh no, an error occurred!") ) .commit(); } } </code></pre> <p>This way if detached and re-attached the object state can be stored through the arguments. Much like bundles attached to Intents.</p> <p><strong>Reason - Extra reading</strong></p> <p>I thought I would explain why for people wondering why.</p> <p>If you check: <a href="https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/app/Fragment.java" rel="noreferrer">https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/app/Fragment.java</a></p> <p>You will see the <code>instantiate(..)</code> method in the <code>Fragment</code> class calls the <code>newInstance</code> method:</p> <pre><code>public static Fragment instantiate(Context context, String fname, @Nullable Bundle args) { try { Class&lt;?&gt; clazz = sClassMap.get(fname); if (clazz == null) { // Class not found in the cache, see if it's real, and try to add it clazz = context.getClassLoader().loadClass(fname); if (!Fragment.class.isAssignableFrom(clazz)) { throw new InstantiationException("Trying to instantiate a class " + fname + " that is not a Fragment", new ClassCastException()); } sClassMap.put(fname, clazz); } Fragment f = (Fragment) clazz.getConstructor().newInstance(); if (args != null) { args.setClassLoader(f.getClass().getClassLoader()); f.setArguments(args); } return f; } catch (ClassNotFoundException e) { throw new InstantiationException("Unable to instantiate fragment " + fname + ": make sure class name exists, is public, and has an" + " empty constructor that is public", e); } catch (java.lang.InstantiationException e) { throw new InstantiationException("Unable to instantiate fragment " + fname + ": make sure class name exists, is public, and has an" + " empty constructor that is public", e); } catch (IllegalAccessException e) { throw new InstantiationException("Unable to instantiate fragment " + fname + ": make sure class name exists, is public, and has an" + " empty constructor that is public", e); } catch (NoSuchMethodException e) { throw new InstantiationException("Unable to instantiate fragment " + fname + ": could not find Fragment constructor", e); } catch (InvocationTargetException e) { throw new InstantiationException("Unable to instantiate fragment " + fname + ": calling Fragment constructor caused an exception", e); } } </code></pre> <p><a href="http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#newInstance()" rel="noreferrer">http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#newInstance()</a> Explains why, upon instantiation it checks that the accessor is <code>public</code> and that that class loader allows access to it.</p> <p>It's a pretty nasty method all in all, but it allows the <code>FragmentManger</code> to kill and recreate <code>Fragments</code> with states. (The Android subsystem does similar things with <code>Activities</code>).</p> <p><strong>Example Class</strong></p> <p>I get asked alot about calling <code>newInstance</code>,(do not confuse this with the class method. This whole class example should show the usage.</p> <pre><code>/** * Created by chris on 21/11/2013 */ public class StationInfoAccessibilityFragment extends BaseFragment implements JourneyProviderListener { public static final StationInfoAccessibilityFragment newInstance(String crsCode) { StationInfoAccessibilityFragment fragment = new StationInfoAccessibilityFragment(); final Bundle args = new Bundle(1); args.putString(EXTRA_CRS_CODE, crsCode); fragment.setArguments(args); return fragment; } // Views LinearLayout mLinearLayout; /** * Layout Inflater */ private LayoutInflater mInflater; /** * Station Crs Code */ private String mCrsCode; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mCrsCode = getArguments().getString(EXTRA_CRS_CODE); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mInflater = inflater; return inflater.inflate(R.layout.fragment_station_accessibility, container, false); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mLinearLayout = (LinearLayout)view.findViewBy(R.id.station_info_accessibility_linear); //Do stuff } @Override public void onResume() { super.onResume(); getActivity().getSupportActionBar().setTitle(R.string.station_info_access_mobility_title); } // Other methods etc... } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload