This afternoon tangled up a day, how to pass the parameters to fragment, in fact, a lot of information online,
Getactivity (). Getintent (). Getstringextra (GetString (R.string.bloger));
Mostly the method above. Because my actual situation is very special, cause can not directly use the above method.
The activity of my fragment attach is initiated in Singletask, that is, jumping from other activity to the activity is not re-instantiated, and the Getintent () method cannot get the arguments passed. However, you can use the
protected void Onnewintent (Intent Intent)
The intent object in the method gets the parameter. However, this intent object cannot be passed directly to fragment, and the last thought is to share the intent object with Getintent () so that you can call fragment () directly in getactivity. Getintent () Gets the parameter.
The methods in activity are as follows:
<span style= "White-space:pre" ></span>//when the startup mode is Singletask, when it is restarted, call @overrideprotected void OnNewIntent ( Intent Intent) {super.onnewintent (Intent); int initposition = Intent.getintextra (getString (r.string.init_position), Constants.def_blog_type. Blogerblog); LOG.I (TAG, "onnewintent initposition=" + initposition); Indicator.setcurrentitem (initposition); GetIntent (). Putextras (intent);}
The methods for obtaining parameters in fragment are as follows:
@Overridepublic void Onresume () { super.onresume (); LOG.I (TAG, "Onresume"); Activity activity = getactivity (); if (activity! = null) {String Bloger = Activity.getintent (). Getstringextra (GetString (R . String.bloger)); if (! Textutils.isempty (Bloger) && bloger.equals (Csdnapplication.getinstance (). Getcurrentblogerid ())) { Bloglistview.startrefresh (); Start Refresh}} Mobclickagent.onpagestart ("Blogerblogfrag");//Statistics Page}
Directly affixed to the project code, not specifically written demo, so can not directly upload the project source. If in doubt, welcome to the discussion!
Activity passing parameters to fragment