The static variable in the Android 4.1 app retains its last assignment even after the app exits.
Public classMainactivityextendsActivity {PrivateTextView TV; Private Static inti=10; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); TV=(TextView) Findviewbyid (R.ID.TEXTVIEW1); Button Button=(Button) Findviewbyid (R.id.button1); Button.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {i+ = 10; } }); Button Button2=(Button) Findviewbyid (R.id.button2); Button2.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Tv.settext ("I is:" +i); } }); }}
When the app starts for the first time, clicking Button 2 displays the initial value of I.
Click button 1, add I 10, exit the app, start the app again, click Button 2, show I The value is: 20.
Note: After the program exits, the value of I is overwritten and unrecoverable. I also will not be re-assigned at the next startup by the program recycle.
Static variables in the Android 4.1 app are not erased even after the app exits