The Activity is started in singleTask mode. The solution for intent value passing is singletaskintent.

Source: Internet
Author: User

The Activity is started in singleTask mode. The solution for intent value passing is singletaskintent.

Reprinted please indicate the source, thank you http://blog.csdn.net/harryweasley/article/details/46557827

Because there is a message push function in the project, each time a message is pushed, FunctionActivity is enabled. In order to avoid re-enabling it, this Activity appears multiple times when you step back, change the Startup Mode of the Activity to singleTask.

In this way, the onNewIntent (Intent intent) method is called multiple times after the Activity is started.

When the activity transmits data through intent, if the activity is not started, the intent data will be obtained through getIntent () in the newly started activity. if the activity to be started already exists, the intent obtained through the getInten () method is the original intent of the started activity. in other words, intent data is not updated. in this way, the intent data obtained in the started activity is old data. if you want to obtain new data from intent each time, you need to call setIntent (Intent) in the onNewIntent (intent) method to set the latest intent. as follows:

@ Overrideprotected void onNewIntent (Intent intent) {super. onNewIntent (intent); Log. e ("tag", "onNewINtent executed"); setIntent (intent); String ringName = intent. getStringExtra ("ringName"); Log. e ("tag", ringName + "passed value"); if (ringName! = Null) {pager. setCurrentItem (1 );}}



Of course, if the Startup Mode of the activity is standard, re-create a new activity every time. In this way, the intent is the latest. You do not need to use setIntent to update the intent.


In my project, there are four Fragment items in FunctionActivity. In this way, I will not instantiate the function from other activities, and the latest intent cannot be obtained through the getIntent () method. Or in the onNewIntent method, share the updated intent through getIntent (). putExtras (intent);, as shown below:


@ Overrideprotected void onNewIntent (Intent intent) {super. onNewIntent (intent); Log. e ("tag", "onNewINtent executed"); setIntent (intent); getIntent (). putExtras (intent );}



In this way, you can call

@ Overridepublic void onResume () {super. onResume (); // The first time you enter this page, the following method will not be executed, because ringName is nullString ringName = getActivity (). getIntent (). getStringExtra ("ringName"); if (ringName! = Null) {newSound. setText (ringName); Log. e ("tag", ringName + "value to be saved"); SharedPreferenceUtil. setString (getActivity (), SharedPreferenceUtil. RINGTONE_NAME, ringName );}}

Note that Fragment must be called in the onResume method.



Related Article

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.