There is something similar to the session under Android, called Application, getapplication () usage

Source: Internet
Author: User

There is something similar to the session on Android, called Application.
There is something similar to the session on Android, called Application.

1, you can create a new class, for example: Hellowordapplication.java must extends application (name with you)
In this class, set the data variables you want to be global, for example: private String loginName;
It then generates its get, set method.
2. Configure your application class in the Androidmanifest.xml file as follows:
<application android:icon= "@drawable/icon" android:label= "@string/app_name" Android:name= "hellowordapplication ">
<activity >
Slightly
</activity>
</application>
is to add the attribute configuration of android:name= "Hellowordapplication" to the <application> tag.
3, in the use of the time:
Hellowordapplicationapplication = (hellowordapplication) getapplication ();
Application.setloginname ("Baidu");
Then will "Baidu" saved to the application, other places to use when, application.getloginname ();

Android Getapplication () use

Java code
  1. Package com.hyzing;
  2. Import android.app.Application;
  3. Public class Mysystemappcation extends application{
  4. private int curindex;
  5. public int Getcurindex () {
  6. return curindex;
  7. }
  8. public void Setcurindex (int curindex) {
  9. this.curindex = Curindex;
  10. }
  11. @Override
  12. public void OnCreate () {
  13. super.oncreate ();
  14. }
  15. @Override
  16. public void Onterminate () {
  17. super.onterminate ();
  18. }
  19. }
Java code
  1. Package com.hyzing;
  2. Import android.app.Activity;
  3. Import android.content.Intent;
  4. Import Android.os.Bundle;
  5. Import Android.util.Log;
  6. Public class Systemtestactivity extends Activity {
  7. @Override
  8. public void OnCreate (Bundle savedinstancestate) {
  9. super.oncreate (savedinstancestate);
  10. //setcontentview (R.layout.main);
  11. Mysystemappcation application = (mysystemappcation) This
  12. . Getapplication ();
  13. System.out.println ("++++++++++++++" +application);
  14. LOG.I ("Data", "===============" + Application.getcurindex ());
  15. Application.setcurindex (5);
  16. Intent Intent = new Intent ();
  17. Bundle bundle = new bundle ();
  18. Bundle.putstring ("CheckIn", "0");
  19. Bundle.putboolean ("Manageruser", true);
  20. Intent.putextras (bundle);
  21. Intent.setclass (<span style="color: #ff0000;" >systemtestactivity. This</span>, printactivity.  class);
  22. StartActivity (Intent);
  23. }
  24. }
Java code
  1. Package com.hyzing;
  2. Import android.app.Activity;
  3. Import android.content.Intent;
  4. Import Android.os.Bundle;
  5. Import Android.util.Log;
  6. Public class Printactivity extends Activity {
  7. @Override
  8. protected void OnCreate (Bundle savedinstancestate) {
  9. <span style="color: #ff0000;" >super.oncreate (savedinstancestate);</span>
  10. Mysystemappcation application = (mysystemappcation) This
  11. . Getapplication ();
  12. LOG.I ("Data", " " "+ Application.getcurindex ());
  13. Application.setcurindex (6);
  14. Intent Intent = new Intent ();
  15. Bundle bundle = new bundle ();
  16. Bundle.putstring ("CheckIn", "0");
  17. Bundle.putboolean ("Manageruser", true);
  18. Intent.putextras (bundle);
  19. Intent.setclass (printactivity. This, printagainactivity.  class);
  20. StartActivity (Intent);
  21. }
  22. }
Java code
  1. Package com.hyzing;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Import Android.util.Log;
  5. Public class Printagainactivity extends Activity {
  6. @Override
  7. protected void OnCreate (Bundle savedinstancestate) {
  8. <span style="color: #ff0000;" >super.oncreate (savedinstancestate);</span>
  9. Final Mysystemappcation application = (mysystemappcation) This
  10. . Getapplication ();
  11. LOG.I ("Data", " " "+ Application.getcurindex ());
  12. }
  13. }
Java code
    1. <?xml version="1.0" encoding="Utf-8"?>
    2. <manifest xmlns:android="Http://schemas.android.com/apk/res/android"
    3. package="com.hyzing"
    4. android:versioncode="1"
    5. Android:versionname="1.0" >
    6. <uses-sdk android:minsdkversion="/> "
    7. <application android:name=". Mysystemappcation "
    8. android:icon="@drawable/ic_launcher"
    9. Android:label="@string/app_name" >
    10. <activity
    11. Android:name=". Systemtestactivity "
    12. Android:label="@string/app_name" >
    13. <intent-filter>
    14. <action android:name="Android.intent.action.MAIN"/>
    15. <category android:name="Android.intent.category.LAUNCHER"/>
    16. </intent-filter>
    17. </activity>
    18. <activity
    19. Android:name=". Printactivity "
    20. Android:label="@string/app_name" >
    21. </activity>
    22. <activity
    23. Android:name=". Printagainactivity "
    24. Android:label="@string/app_name" >
    25. </activity>
    26. </application>
    27. </manifest>

There is something similar to the session under Android, called Application, getapplication () usage

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.