Activity
When your is creating a new instance of activity via Intent, you can pass some extra data to the activity. Data is stored in Bundle and can is retrieved by calling getintent (). Getextras (). It ' s a good practise to implement static method newintent () which returns a new Intent that can is used to start the Activity. This is the compile time checking for the arguments passed to the Activity. This pattern was suitable for Service and broadcast as well.
Bundle is also used if the Activity was being re-initialized (e.g. because of configuration changes) for keeping the CU Rrent state of the instance. You can supply some data in onsaveinstancestate (Bundle) and retrieve them back in onCreate (Bundle) method or onrestoreinstancestate (Bundle). Main difference between these methods is that onrestoreinstancestate (Bundle) is called after OnStart (). Sometimes it ' s convenient to restore data here after all of the initialization have been done. Another purpose could is allowing subclasses to decide whether to use your default implementation.
See example code below. Note that extra_product_id constant are public. That's because we could need it in a Fragment encapsulated in the this Activity.
public class Exampleactivity extends Activity {public static final String extra_product_id = "product_id"; public static final String extra_product_title = "Product_title"; private static final String saved_pager_position = "Pager_position"; public static Intent Newintent (context context, string productId, String producttitle) {Intent Intent = new Intent (context, Exampleactivity.class); Extras Intent.putextra (extra_product_id, productId); Intent.putextra (Extra_product_title, ProductTitle); return intent; } @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_my); Restore saved state if (savedinstancestate! = null) {handlesavedinstancestate (savedinstancestate); }//Handle intent extras Bundle extras = Getintent (). Getextras (); if (extras! = null) {Handleextras (extras); }} @Override public void Onsaveinstancestate (Bundle outstate) {//Save current instance state Super.onsaveinstancestate (outstate); TODO} @Override public void Onrestoreinstancestate (Bundle savedinstancestate) {//restore saved state Super.onrestoreinstancestate (savedinstancestate); if (savedinstancestate! = null) {//TODO}} private void Handlesavedinstancestate (Bundle Savedin Stancestate) {//Todo} private void Handleextras (Bundle extras) {//Todo}} Fragment
When your is creating a new instance of Fragment, you can pass arguments through setarguments (Bundle) method. Data can be retrieved withgetarguments () method. It would is a mistake to supply initialization data through an overloaded constructor. Fragment instance can be re-created (e.g. because of the configuration changes) so you would lose data, because constructor WI Th extra parameters is not the called when Re-initializing Fragment. Only empty constructor is called. Best-by-solve this problem is implementing static Creator method newinstance () which returns a new instance of Fragment and sets the arguments via setarguments (Bundle).
Fragment state can saved using onsaveinstancestate (Bundle) method. It's similar to the Activity. Data can be restored in OnCreate (bundle), Oncreateview (Layoutinflater, ViewGroup, bundle), onactivitycreated (Bundle) or onviewstaterestored (Bundle) methods.
Fragment have also access to the Intent extras which were passed during creating the Activity instance. You can get the extra data by Callinggetactivity (). Getintent (). Getextras ().
see example code below.
public class Examplefragment extends Fragment {private static final String argument_product_id = "product_id"; private static final String saved_list_position = "List_position"; public static examplefragment newinstance (String productId) {examplefragment fragment = new Examplefragment (); Arguments bundle arguments = new bundle (); Arguments.putstring (argument_product_id, productId); Fragment.setarguments (arguments); return fragment; The public examplefragment () {} @Override the public void OnCreate (Bundle savedinstancestate) {Super.oncrea Te (savedinstancestate); Handle fragment arguments Bundle arguments = getarguments (); if (arguments! = null) {handlearguments (arguments); }//Restore Saved state if (savedinstancestate! = null) {handlesavedinstancestate (savedinstance State); }//Handle intent extras Bundle Extras = GetactivitY (). Getintent (). Getextras (); if (extras! = null) {Handleextras (extras); }} @Override public void Onsaveinstancestate (Bundle outstate) {//Save current instance state SUP Er.onsaveinstancestate (outstate); TODO} private void handlearguments (Bundle arguments) {//TODO} private void Handlesavedinstancest ATE (bundle savedinstancestate) {//Todo} private void Handleextras (bundle extras) {//Todo} }