• 5

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Passing data between activities in Android

How do you pass data between activities in an Android application?

in your current activity, create an intent

Intent i = new Intent(getApplicationContext(), ActivityB.class);
i.putExtra(key, value);

then in the other activity, retrieve those values.

Bundle extras = getIntent().getExtras(); 
if(extras !=null) {
    String value = extras.getString(key);
  • 56
Reply Report
    • I currently have insufficient rep to vote this down, but it's worth noting that, while this will work in some cases, it is not the only way to do it, and is not always the best, simplest, quickest or most efficient. Emre's answer (which has luckily been selected as the right answer despite a huge difference in votes) links to a variety of methods, from which you can choose the best solution for your app.

Use a global class:

public class GlobalClass extends Application
    private float vitamin_a;

    public float getVitaminA() {
        return vitamin_a;

    public void setVitaminA(float vitamin_a) {
        this.vitamin_a = vitamin_a;

You can call the setters and the getters of this class from all other classes. Do do that, you need to make a GlobalClass-Object in every Actitity:

GlobalClass gc = (GlobalClass) getApplication();

Then you can call for example:

  • 1
Reply Report

Put this in your secondary activity

SharedPreferences preferences =getApplicationContext().getSharedPreferences("name", MainActivity.MODE_PRIVATE);

android.content.SharedPreferences.Editor editor = preferences.edit();

editor.putString("name", "Wally");

Put this in your MainActivity

SharedPreferences preferences = getApplicationContext().getSharedPreferences("name", MainActivity.MODE_PRIVATE);


Toast.makeText(getApplicationContext(), preferences.getString("name", "null"), Toast.LENGTH_LONG).show();

  • 0
Reply Report