Static Keywords of Android static variables

Source: Internet
Author: User

Today I was trying to test a question by creating a subthread in activity. Then stop activity a quickly. I want to know if this sub-thread will continue running. The answer is yes. This child thread is like having nothing to do with activity A. It will run until it is finished.

The related code is as follows:

Package com. Android. test;

Import Android. App. activity;
Import Android. content. intent;
Import Android. OS. Bundle;

Public class activityandthredactivity extends activity {

/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Mythread Mt = new mythread ();
Mt. Start ();
Intent intent = new intent (this, next. Class );
Startactivity (intent );
Finish ();
}
}

Package com. Android. test;

Import Android. App. activity;
Import Android. content. intent;
Import Android. OS. Bundle;

Public class activityandthredactivity extends activity {

/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Mythread Mt = new mythread ();
Mt. Start ();
Intent intent = new intent (this, next. Class );
Startactivity (intent );
Finish ();
}
} Nothing special. After starting another activity, close yourself.

The thread code is as follows:

/**
*
*/
Package com. Android. test;

Import Android. util. log;

/**
* @ Author maqinghua
*
*/
Public class mythread extends thread {

@ Override
Public void run (){
// Todo auto-generated method stub
Super. Run ();
Try {
Sleep (5000 );
INF. getinstance (). info. Add ("thread success ");
} Catch (interruptedexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();

Log. D ("test", "thread error ");
INF. getinstance (). info. Add ("thread error ");
}
Log. D ("test", "thread success ");
}

}

An INF Information Class is used to record information and output, as follows:

/**
*
*/
Package com. Android. test;

Import java. util. arraylist;

/**
* @ Author maqinghua
*
*/
Public class inf {

Private Static INF instance;
Public arraylist <string> Info;

Private Inf (){
Info = new arraylist <string> ();
}
Public static INF getinstance (){
If (instance! = NULL ){
Return instance;
} Else {
Instance = New INF ();
Return instance;
}
}

Public String inftostring (){

If (info! = NULL ){
Stringbuilder sb = new stringbuilder ();
For (INT I = 0; I <info. Size (); I ++ ){
SB. append (info. Get (I ));
}
Return sb. tostring ();
} Else {
Return "";
}
}

}

Next class

/**
*
*/
Package com. Android. test;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. util. log;
Import Android. View. keyevent;
Import Android. widget. textview;

/**
* @ Author maqinghua
*
*/
Public class next extends activity {

@ Override
Protected void oncreate (bundle savedinstancestate ){
// Todo auto-generated method stub
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Log. D ("test", "oncreate ()");
INF. getinstance (). info. Add ("oncreate ()");
Textview TV = (textview) findviewbyid (R. Id. Main );
String text = inf. getinstance (). inftostring ();
TV. settext (text );
}

@ Override
Public Boolean onkeydown (INT keycode, keyevent event ){
// Todo auto-generated method stub
If (keycode = keyevent. keycode_back ){
Finish ();
}
Return super. onkeydown (keycode, event );
}

}

There is also a layout file main

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Orientation = "vertical">

<Textview
Android: Id = "@ + ID/main"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textsize = "20sp"
Android: text = "@ string/Hello"/>

</Linearlayout>

During the test, I found something interesting. When I press the return key to exit the application and then enter the application. I was surprised to find that the information class could remember the information before the last application exited. Does the INF Information Class disappear with the disappearance of the activity? I tried again several times and found that it was not. Unless we uninstall the application or disable it forcibly in program management, it (INF) will be recycled by the system.

If you want to optimize your applications, remember to delete the static classes or attributes you want to clear when you want to exit.

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.