How does Android receive parameters transmitted in putExtra mode for an Activity loaded in singleInstance mode?

Source: Internet
Author: User

During development, we often specify the Activity loading mode as singleInstance in AndroidManifest. xml, as follows:


[Javascript] <activity android: name = "ImgInfo" android: label = "@ string/app_name"
Android: screenOrientation = "portrait" android: launchMode = "singleInstance">
</Activity>
<Activity android: name = "ImgInfo" android: label = "@ string/app_name"
Android: screenOrientation = "portrait" android: launchMode = "singleInstance">
</Activity>

Open the Activity and PASS Parameters as follows:


[Plain] Intent I = new Intent (this, ImgInfo. class );
I. putExtra ("id", mPhotoId );
StartActivity (I );
Intent I = new Intent (this, ImgInfo. class );
I. putExtra ("id", mPhotoId );
StartActivity (I );


If you do not understand the principles of singleInstance, We will write the following code in the onCreate method of Activity ImgInfo:


[Plain] Bundle bud = getIntent (). getExtras ();
If (bud! = Null & bud. containsKey ("id ")){
MPhotoId = bud. getInt ("id ");
}
Bundle bud = getIntent (). getExtras ();
If (bud! = Null & bud. containsKey ("id ")){
MPhotoId = bud. getInt ("id ");
}
If you pass different IDs, you will find that, except for the first time you can receive them correctly, none of them seem to work. What should I do? Override the onNewIntent method. The basic code is as follows:


[Java] @ Override
Protected void onNewIntent (Intent intent ){
Super. onNewIntent (intent );
SetIntent (intent );
InitArg ();
}
Private InitArg (){
Bundle bud = getIntent (). getExtras ();
If (bud! = Null & bud. containsKey ("id ")){
MPhotoId = bud. getInt ("id ");
}
// Other operations
}
@ Override
Protected void onNewIntent (Intent intent ){
Super. onNewIntent (intent );
SetIntent (intent );
InitArg ();
}
Private InitArg (){
Bundle bud = getIntent (). getExtras ();
If (bud! = Null & bud. containsKey ("id ")){
MPhotoId = bud. getInt ("id ");
}
// Other operations
}
Why? This requires an in-depth understanding of the Activity loading mechanism and related callback functions. This blog only records small issues encountered during work and does not elaborate on the principles. If necessary, please query the relevant information!

From the pure soul

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.