Activity passing parameters to fragment

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.