0

i searched for way to store a global variables that will be shared with all the activities.

i found this:

How to declare global variables in Android?

and i created a class that extends application and i declare this application in the manifest file.

however i have a problem:

i cant decalre more than one application in the manifest file...

therefore if i want to declare two applications, what can i do?

here is my manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sa"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>



    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="10" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA"/>

    <permission
    android:name="com.example.sa.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />



    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
        <uses-permission android:name="com.example.sa.permission.MAPS_RECEIVE"/>
        <uses-permission android:name="android.permission.READ_PHONE_STATE"/>




    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" 
         android:debuggable="true"
        android:name=".FriendPickerApplication">



        <activity
            android:name="com.example.sa.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>

         <!--  Entry for RegisterActivity.class -->
        <activity android:name=".RegisterActivity"
                  android:label="Register New Account"></activity>
        <meta-data android:value="@string/app_id" android:name="com.facebook.sdk.ApplicationId"/>
        <activity android:label="@string/app_name" android:name="com.facebook.LoginActivity"></activity>
        <activity android:name=".ProfileActivity" android:label="Profile page"></activity>

        <meta-data
              android:name="com.google.android.maps.v2.API_KEY"
              android:value="xxxx"/>


        <activity android:name="DashBoardActivity"></activity>
        <activity android:name="AllItemsActivity"></activity>
        <activity android:name="itemCategory"></activity>
        <activity android:name="ItemMapActivity"></activity>

    </application>

</manifest>

and i also want to delare a class that extends application that will store the shared variables:

<application android:name="MyApp" 
   android:icon="@drawable/icon" 
   android:label="@string/app_name2"></application>

and this is the class:

class MyApp extends Application {

  private String myState;

  public String getState(){
    return myState;
  }
  public void setState(String s){
    myState = s;
  }
}

class Blah extends Activity {

  @Override
  public void onCreate(Bundle b){
    ...
    MyApp appState = ((MyApp)getApplicationContext());
    String state = appState.getState();
    ...
  }
}

thanks alot

Community
  • 1
  • 1
Adir Rahamim
  • 453
  • 1
  • 12
  • 31
  • Why not have several variables: ie myState1 and myState2, and get one or the other? – Dan Osipov Apr 23 '13 at 16:23
  • i need to pass alot of variables each time between activities, i want to declare them to be global and shared between all activities.. – Adir Rahamim Apr 23 '13 at 16:27
  • Why do you need two separate applications? Why can't you have multiple activities all contained inside a single application? – Rob Watts Apr 23 '13 at 16:38
  • i read that i can use this application as a global variable, is there another way to implement a global variable which will be shared between all activities? – Adir Rahamim Apr 23 '13 at 16:42

2 Answers2

1

You can not declare more than one application.

You can have more variables in you application, but application is a singleton class.

wangyif2
  • 2,843
  • 2
  • 24
  • 29
0

To define Global Variable you can make use of static Keyword:

public static String myState;
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39