[:en]Android: Global Variables[:]

[:en]Global variables

Step 1: GlobalClass.java


package com.example.cambridge.gloablvariable;

import android.app.Application;

/**
 * Created by cambridge on 16/2/16.
 */
public class GlobalClass extends Application {

    private String name;
    private String email;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}


Step 2: activity_main.xml


    

Step 3: activity_second.xml


    

    

Step 4: activity_third.xml



    



Step 5: MainActivity.java

package com.example.cambridge.gloablvariable;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Global variable
        final GlobalClass globalVariable = (GlobalClass)getApplicationContext();

        globalVariable.setName("Context variable name");
        globalVariable.setEmail("xxx@gmail.com");


    }

    public void startSecondActivity (View v){
        Intent i = new Intent(getBaseContext(),SecondActivity.class);
        startActivity(i);


    }

}

Step 6: SecondActivity.java

package com.example.cambridge.gloablvariable;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import org.w3c.dom.Text;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        // Global variable

        TextView showNameEmail = (TextView)findViewById(R.id.show_global);
        final GlobalClass globalVariable = (GlobalClass)getApplicationContext();

        final String name = globalVariable.getName();
        final String email = globalVariable.getEmail();

        String showString = "Name:" + name + "\nEmail: " + email;

        showNameEmail.setText(showString);


    }

    public void showThirdActivity(View v){
        Intent i = new Intent(getBaseContext(),ThirdActivity.class);
        startActivity(i);

    }
}

Step 7: ThirdActivity.java

package com.example.cambridge.gloablvariable;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class ThirdActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);

        // Global variable

        TextView showNameEmail = (TextView)findViewById(R.id.thirdshow_global);
        final GlobalClass globalVariable = (GlobalClass)getApplicationContext();

        final String name = globalVariable.getName();
        final String email = globalVariable.getEmail();

        String showString = "Name:" + name + "\nEmail: " + email;

        showNameEmail.setText(showString);

    }
}

Step 8: Manifest.xml





    
        
            
                

                
            
        
        
        
        
        
    


[:]

Print Friendly, PDF & Email
Scroll to Top