Wednesday, May 30, 2012

Pass Data Between Activities in Android?: Part 4


In Android, we can inherit the Application class to achieve application-level global variables, this method of global variables is relatively static class more secure, and will be released until after the application's All Activities destroy out.

In the Activity, we can use getApplication() method to obtain the Application, it is representative of the class of our application, it can get the theme of the current application, the content in the resource file, this class is more flexible a feature can we have inherited, to add our own global properties.

All the Activity of the same application can take the value of these global variables, in other words, we change the value of these global variables in an Activity, then in the same value will change in an application Activity. The following example details the application steps:

STEP 1:: Write Application Subclass


public class MyApplication extends Application { //Variable we want to share to All Activities in Appliction private static final String NAME = "MyApplication"; private String name; @Override public void onCreate() { super.onCreate(); setName(NAME); //Initialize global variables } //Getter Method public String getName() { return name; } //Setter Method public void setName(String name) { this.name = name; } }


STEP 2: In the Manifest.xml Register MyApplication as:


<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hisoft.app" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:name=".MyApplication"> <activity android:name=".MyFirstActivity" android:label="@string/app_name">


STEP 3: In the MyFirstActivity :


public class MyFirstActivity extends Activity { private MyApplication app; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); app = (MyApplication)getApplication(); //Get Application //Access global value Log.e("MyFirstActivityOriginal", app.getName()); app.setName("Android Android"); //Set global value Log.e("MyFirstActivityChanged", app.getName()); Intent intent = new Intent(); intent.setClass(this, MySecondActivity.class); startActivity(intent); } }


STEP 4: In the MySecondActivity :


public class MySecondActivity extends Activity { private MyApplication app; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //Get Application app = (MyApplication)getApplication(); //Access global value Log.e("MySecondActivity", app.getName()); } }




Download Source Code: Applicationclass









1 comments:

macombpacker said...

The Casino Review - DRM CD
The Casino is an exciting 거제 출장샵 and easy 서산 출장안마 way to get a 강원도 출장안마 great welcome bonus, but we've taken a closer look at how it works and some 고양 출장샵 of the 광주 출장마사지 best things to know about

Post a Comment

 
Powered by Blogger