To use global variables in Android apps, in addition to static variables of public, you can also use android. app. Application in a more elegant way.
When the Application is started, the system creates a PID, that is, the process ID, and all the activities will run on this process. In this case,
When the global variables are initialized, all the activities of the same application can obtain the values of these global variables.
For example, save the logon status in Application
AndroidMenifest. xml
<application
android:name=".CombankDroid"
android:icon="@drawable/first_aid"
android:label="@string/app_name" >
</application>
CombankDroid. java
public class CombankDroid extends Application {
private boolean isLogin;
public boolean isLogin(){
return isLogin
}
public void setIsLogin(boolean b){
isLogin = b;
}
}
Anyhow. java
class Anyhow extends Activity {
@Override
public void onCreate(Bundle b){
...
CombankDroid combankDroid = ((CombankDroid)getApplicationContext());
//CombankDroid combankDroid = (CombankDroid) getApplication();
// Don't start the main activity if we don't have credentials
if (!combankDroid.isLogin()) {
redirectToLoginActivity();
} else {
loadUi();
}
...
}
...
}