Skip to content

Declare an Android Global Variable in Kotlin

How could create the Android global variable keep remain values around the life cycle of the application regardless of which activity or fragment or other component is running? You can extend the base android.app.Application class and add member variables like so:

How to Declare a Android Global Variable in Kotlin example tutorial

You can do it with Extend an Application class in Android, where an Application class is a base class for maintaining a global application state.

Let’s Build a simple Android app for the android global variable:

This example will give you a clear idea of how to make a global variable in kotlin.

Step 1. Create a new project “Build Your First Android App in Kotlin
Step 2. Create a new class ” MyApplication.kt ” in Kotlin

Extend Application class and add a variable (A android Global Variable)

package `in`.eyehunt.androidglobalvariablekotlin

import android.app.Application

class MyApplication : Application() {
    var globalVar = "I am Global Variable"
}
Step 3. Add ” MyApplication.kt ” class in AndroidManifest file as an attribute of <application> tag:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.eyehunt.androidglobalvariablekotlin">

    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Step 3. Access the global data or variable from any Activity by custom application class

Add this following code in ” MainActivity.kt

package `in`.eyehunt.androidglobalvariablekotlin

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //get value of global var used getApplication
        var mApp = MyApplication()
        var strGlobalVar = mApp.globalVar

        Toast.makeText(this, strGlobalVar, Toast.LENGTH_LONG).show()
    }
}
Step 6. Run the application, in an emulator or On your Android device

Output screenshot Android Global Variable in Kotlin example :

How to Declare a Android Global Variable in Kotlin example output

Download & View source code

Bouns: How to define a global variable in android java (Android Studio example) 

Do comment if you have any doubts and suggestions on this tutorial

Note: This example (Project) is developed in Android Studio 3.1.3. Tested on Android 9 ( Android-P), compile SDK version API 26: Android 8.0 (Oreo)

MinSdkVersion=”15″

TargetSdkVersion=”27″

Coding in Kotlin


8 thoughts on “Declare an Android Global Variable in Kotlin”

  1. You can actually have global variable, belonging to package.

    package com.example.android.class3
    var MyVar = “Hi”

    To use it you need to import it:

    import com.example.android.class3.MyVar

    Please check how it works before writing tutorials.

    My question is now when it executes if it has function invocation (at app start maybe or package first import)?

  2. I have followed this and the globalVariable value is available globally

    However, I do not appear to be able to update the Global value of the string.

    Tha value stays the same as the initial value, yet within an individual Kotlin File the value can be reassigned and displayed locally. But as soon as the layout and kt file changes the string value reverts to the initial myApplication value.

    I hardly call this a GLOBAL variable, more a Global Value and Local Variable

    What I want to have is a true Global Variable, which can be reassigned for use in another layout.

    Or have I misunderstood what you intend?

Leave a Reply

Your email address will not be published. Required fields are marked *