Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Best way to transfer variables from one activity to the next in Android?

I'm working on an Android app that provides financial recommendations based on your income and expenses.

I need a way to define global variables. I've attempted the following: http://stackoverflow.com/questions/1944656/android-global-variable

But to no avail. I put the code in my first activity and added the name to the manifest, but just got a cast class exception when attempting to pull the set method from the class.

Anyone have an idea as to why this is happening?

package ericleeconklin.costoflivingcalculator;

import android.app.Application;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import java.lang.Double;


public class EnterIncome extends ActionBarActivity {

    public Double myIncome;

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_enter_income, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public class myVariables extends Application {

        private Double income;

        public Double getIncome() {
            return income;
        }

        public void setIncome(Double income) {
            this.income = income;
        }

        private Double rentMortgage;

        public Double getrentMortgage() {
            return rentMortgage;
        }

        public void setrentMortgage(Double rentMortgage) {
            this.rentMortgage = rentMortgage;
        }

        private Double insurance;

        public Double getinsurance() {
            return insurance;
        }

        public void setinsurance(Double insurance) {
            this.insurance = insurance;
        }

        private Double utilities;

        public Double getutilities() {
            return utilities;
        }

        public void setutilities(Double utilities) {
            this.utilities = utilities;
        }

        private Double phoneInternet;

        public Double getphoneInternet() {
            return phoneInternet;
        }

        public void setphoneInternet(Double phoneInternet) {
            this.phoneInternet = phoneInternet;
        }

        private Double food;

        public Double getfood() {
            return food;
        }

        public void setfood(Double food) {
            this.food = food;
        }

        private Double carPayment;

        public Double getcarPayment() {
            return carPayment;
        }

        public void setcarPayment(Double carPayment) {
            this.carPayment = carPayment;
        }

        private Double misc;

        public Double getmisc() {
            return misc;
        }

        public void setmisc(Double misc) {
            this.misc = misc;
        }
    }

    public void getIncome(View view){
        String myIncomeString;
        TextView incomeView = (TextView)findViewById(R.id.enterIncome);
        myIncomeString = incomeView.getText().toString();
        myIncome = Double.parseDouble(myIncomeString);
        ((myVariables) this.getApplication()).setIncome(myIncome);
        Intent myIntent = new Intent(this, EnterExpenses.class);
        startActivity(myIntent);
    }

}

2 Answers

Edited question for source highlighting.

It looks like this is all one file, correct? The issue is that the subclass of Application should be in a separate file that defines your app (such as public class CostOfLivingApplication extends Application) and not an inner class of one activity. If you go this route.

There are other ways to pass values around in Android such as passing Parcelables in the Intent to start the new activity (see the 2nd Stormy app course: http://teamtreehouse.com/library/android-lists-and-adapters) or using the EventBus or Otto libraries.

I created a file MyVariables.java and am still getting the class cast exception on this.

Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to ericleeconklin.costoflivingcalculator.MyVariables at ericleeconklin.costoflivingcalculator.EnterIncome.getIncome(EnterIncome.java:51)

The application subclass has to be declared in the app manifest else it will use the default Application.

<application
    android:name=".MyApplication"
... >

Yes, did it. Still a cast class exception.