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!