The Activity transmits parameters to Fragment.

Source: Internet
Author: User

The Activity transmits parameters to Fragment.


This afternoon, I had a hard day to figure out how to pass parameters to Fragment. In fact, there are a lot of information on the Internet,

getActivity().getIntent().getStringExtra(getString(R.string.bloger));

Most of them are the methods above. Because my actual situation is special, the above method cannot be used directly.

The start method of my Fragment attach Activity is singleTask. That is to say, redirecting from another activity to this activity will not be re-instantiated, And the passed parameters cannot be obtained through the getIntent () method. However, you can use

protected void onNewIntent(Intent intent)

The intent object in the method gets the parameters. However, this intent object cannot be directly passed to Fragment. The last thought is to share the intent object with getIntent (). In this way, you can directly call getActivity () in Fragment (). getIntent () gets the parameter.

The method in Activity is as follows:

// When the start mode is singletask, @ 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 method for obtaining parameters in Fragment is 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 refreshing} MobclickAgent. onPageStart ("BlogerBlogFrag"); // statistics page}


The code in the directly pasted project does not have a specific demo, so you cannot directly upload the project source code. If you have any questions, welcome to the discussion!




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.