Sharing classes (data) among multiple Android activities -- Application Context

Source: Internet
Author: User

When the same variable is often used in multiple activities, use Bundle to set each call to the Activity. In android, you can use Application context to conveniently use the same variables repeatedly for different activities in the entire Application.
1. Create a new class that inherits from the Application
Package activity.com;

Import android. app. Application;

Public class MyApplication extends Application {

Private String myString = "start ";

Public String getMyString (){

Return myString;

}

Public void setMyString (String myString ){

This. myString = myString;

}

}
2. Call in other activities
Package activity.com;
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. View;
Import android. widget. Button;
Import android. widget. TextView;
Public class FirstActivity extends Activity {
Private TextView myTextView = null;
Private Button myButton = null;
MyApplication myApplication;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. first );
MyTextView = (TextView) findViewById (R. id. myTextView );
MyTextView. setText ("This is first TextView ");
MyButton = (Button) findViewById (R. id. myButton );
MyButton. setText ("First Button ");
MyButton. setOnClickListener (new myOnClickListener ());
MyApplication = (MyApplication) getApplication ();
}
Class myOnClickListener implements View. OnClickListener {
@ Override
Public void onClick (View v ){
String state = myApplication. getMyString ();
Log. d ("TEMP", "First get data:" + state );
MyApplication. setMyString ("First ");
Log. d ("TEMP", "First change data to data:" + "First ");
Intent intent = new Intent ();
Intent. setClass (FirstActivity. this, SecondActivity. class );
FirstActivity. this. startActivity (intent );
}
}
}

Package activity.com;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. View;
Import android. widget. Button;
Import android. widget. TextView;
Public class SecondActivity extends Activity {
Private Button myButton2 = null;
Private TextView myTextView2 = null;
MyApplication myApplication;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. second );
System. out. println ("2-> 1 ");
MyTextView2 = (TextView) findViewById (R. id. myTextView2 );
MyTextView2.setText ("This is second TextView ");
System. out. println ("2-> 2 ");
MyButton2 = (Button) findViewById (R. id. myButton2 );
MyButton2.setText ("Second Button ");
MyButton2.setOnClickListener (new myOnClickListener ());
System. out. println ("2-> 3 ");
MyApplication = (MyApplication) getApplication ();
}
Class myOnClickListener implements View. OnClickListener {
@ Override
Public void onClick (View v ){
String state = myApplication. getMyString ();
Log. d ("TEMP", "Second get data:" + state );
MyApplication. setMyString ("Second ");
Log. d ("TEMP", "Second change data to data:" + "Second ");

}
}
}
3. settings in AndroidManifest. xml:
Add MyApplication to the name tag of application as follows:
<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name" android: name = "MyApplication">
4. After a variable is changed in an Activity, you can obtain the changed value in other activities.
04-25 04:32:23. 361: DEBUG/TEMP (1130): First get data: start
04-25 04:32:23. 361: DEBUG/TEMP (1130): First change data to data: First
04-25 04:32:30. 891: DEBUG/TEMP (1130): Second get data: First
04-25 04:32:30. 891: DEBUG/TEMP (1130): Second change data to data: Second
04-25 04:32:36. 743: DEBUG/TEMP (1130): First get data: Second
04-25 04:32:36. 743: DEBUG/TEMP (1130): First change data to data: First
04-25 04:32:39. 883: DEBUG/TEMP (1130): Second get data: First
04-25 04:32:39. 883: DEBUG/TEMP (1130): Second change data to data: Second
Operation





Source code download: http://www.bkjia.com/uploadfile/2012/0425/20120425020541495.rar




From the love254443233 Column





















 


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.