Created
November 26, 2014 02:01
-
-
Save devrath/5e308b3764f0e49738ef to your computer and use it in GitHub Desktop.
How to use Application class in android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //*****************************************SET these things in Activity*******************************************************// | |
| public class MainActivity extends Activity { | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| AppController appState = ((AppController)getApplicationContext()); | |
| //Set the value in application variable | |
| AppController.setMyState("helloWorld"); | |
| String state = AppController.getMyState(); | |
| Log.d("LOGCAT", state); | |
| } | |
| } | |
| //***********************************SET THESE THINGS in a class that extends application*************************************// | |
| package com.example.createapplicationvariable; | |
| import android.app.Application; | |
| public class AppController extends Application{ | |
| private static String myState; | |
| public static String getMyState() { | |
| return myState; | |
| } | |
| public static void setMyState(String _myState) { | |
| myState =_myState; | |
| } | |
| } | |
| //**************************************Manifest Declaration (mention the application class presence)*********************// | |
| <?xml version="1.0" encoding="utf-8"?> | |
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
| package="com.example.createapplicationvariable" | |
| android:versionCode="1" | |
| android:versionName="1.0" > | |
| <uses-sdk | |
| android:minSdkVersion="8" | |
| android:targetSdkVersion="14" /> | |
| <application | |
| android:name="com.example.createapplicationvariable.AppController" | |
| android:allowBackup="true" | |
| android:icon="@drawable/ic_launcher" | |
| android:label="@string/app_name" | |
| android:theme="@style/AppTheme" > | |
| <activity | |
| android:name=".MainActivity" | |
| android:label="@string/app_name" > | |
| <intent-filter> | |
| <action android:name="android.intent.action.MAIN" /> | |
| <category android:name="android.intent.category.LAUNCHER" /> | |
| </intent-filter> | |
| </activity> | |
| </application> | |
| </manifest> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment