0

I am trying to learn android and i don't know how to pass data from one activity to another

 public void onNextClick(View view){
    Intent intent = new Intent(this,Expenses.class);
    intent.putExtra(EXTRA_SUM, sum);
    startActivity(intent);

    editSalary = (EditText) findViewById(R.id.salayText);
    editIncome = (EditText) findViewById(R.id.incomeText);
    salary = Integer.parseInt(editSalary.getText().toString());
    income = Integer.parseInt(editIncome.getText().toString());
    sum = salary + income;
}


    public void onNextClick2(View view){
    Intent intent2 = new Intent(Expenses.this,Budget.class);
    intent2.putExtra(EXTRA_EXPENSE, expense);
    startActivity(intent2);

    editRent = (EditText) findViewById(R.id.rentText);
    editBills = (EditText) findViewById(R.id.billsText);
    editEveryday = (EditText) findViewById(R.id.everydayText);
    editOther = (EditText) findViewById(R.id.otherText);
    rent = Integer.parseInt(editRent.getText().toString());
    bills = Integer.parseInt(editRent.getText().toString());
    everyday = Integer.parseInt(editEveryday.getText().toString());
    other = Integer.parseInt(editOther.getText().toString());
    expense = rent + bills + everyday + other;
}


    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_budget);

    Intent intent = getIntent();
    summary =  intent.getIntExtra(MainActivity.EXTRA_SUM ,0);
    expenses = intent.getIntExtra(Expenses.EXTRA_EXPENSE, 0);
    totalBudget = summary - expenses;
    textBudget = (TextView) findViewById(R.id.budgetView);
    textBudget.setText(String.valueOf(totalBudget));
}

I am trying to get "sum" from activity 1 and "expenses" from activity 2 and subtract them in activity 3 but i keep getting null

Kostas
  • 3
  • 1
  • 4
    Does this answer your question? [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – Amir Dora. Nov 09 '20 at 23:25

2 Answers2

0

You can't pass a value from first activity to third activity directly.First you have to pass it to first-> second then second-> third.

Else there is a simple solution like you can store it on Shared Preferences Store sum value on shared preferences while moving from first activity to second activity.Then store the expenses value on shared preferences when you move from second to third activity.After that in onCreate method of third activity you can get the both values and subtract.

Sathyan
  • 138
  • 1
  • 10
0

There are many ways to do this.

  1. View Model
  2. Singleton
  3. Pass it in the Intent that launched the new active
apxcode
  • 7,696
  • 7
  • 30
  • 41