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