Application sets global variables and passes values
/** * Rewrite Application, mainly rewrite the onCreate method inside, that is, when it is created, * We asked it to initialize some values. I saw an example in javaeye some time ago, similar to this. * I made some improvements. I heard that foreign developers are used to initializing some global variables with this, as if in Activity * When initializing the global variables in some classes, you will encounter some null pointer exceptions. Of course, I have never encountered them. * If initialized with this method, then those possible errors can be avoided. * * When you start Application, it will create a PID, which is the process ID, and all activities will run on this process. * So when we initialize global variables when the Application is created, can all activities get these * Global variables, furthermore, we have changed the values of these global variables in a certain activity, so in other activities * Does the value change? Is this considered a passing value? * OK, then let's test the following example. . . * @author * */ public class MyApplication extends Application { private String name; @Override public void onCreate() { (); setName(NAME); //Initialize global variables} public String getName() { return name; } public void setName(String name) { = name; } private static final String NAME = "MyApplication"; }
Ok, the application has been created, but we should add the application MyApplication to run in the configuration file and modify it:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:andro package="" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name" android:name=".MyApplication"> That's it,Use the default we have always used beforeApplicationSet it for him to do it ourselvesMyApplication <activity android:name=".MyFirstActivity" android:label="@string/app_name"> <intent-filter> <action android:name="" /> <category android:name="" /> </intent-filter> </activity> <activity android:name=".MySecondActivity"></activity> </application> <uses-sdk android:minSdkVersion="8" /> </manifest>
When the xml configuration file is completed android:name=".MyApplication">, then the process ID is assigned. Next, we will run our Activity
public class MyFirstActivity extends Activity { private MyApplication app; @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); app = (MyApplication) getApplication(); //Get our application MyApplication("MyFirstActivityOriginal", ()); //Take out the global variables we put into the process to see if they are the values we have set before("is cool"); //OK, now we've started to modify("MyFirstActivityChanged", ()); //Look at it again, has this value changed?Intent intent = new Intent(); //More importantly, we can see whether we get the initialized value in other activities or modified(this, ); startActivity(intent); } }
After running above, you will jump to this activity
public class MySecondActivity extends Activity { private MyApplication app; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); app = (MyApplication) getApplication(); //Get the application("MySecondActivity", ()); //Get global value} }
OK, look at the value: Of course I have already run it.
MyFirstActivityOriginal MyApplication MyFirstActivityChanged is cool MySecondActivity is cool
See if it is particularly exciting. Of course, it is possible that when you exit and run again, you will be second and third. . . Time, it is possible that all three outputs are cool, which is the problem that you did not kill the process.
If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!