Search code examples
androidgraphandroid-graphview

How to send values to two different activities at the same time


I am working on a budget app and am having trouble getting my expenses to the class that will create a graph. The user will be able to input an expense along with a check box indicating what they spent money on. When I send the value to MainActivity, it reads the expense and shows the updated budget, however, when I try to read it in my class that will make a graph, the graph shows up empty and is not reading the values. Here is my Expense Activity (Screen 2):

public class ExpensesActivity extends AppCompatActivity {

    EditText editTextExpense;
    Expenses expenses;
    String selectedCheckBox;
    CheckBox personalCheckBox;
    CheckBox commuteCheckBox;
    CheckBox billsCheckBox;
    CheckBox funCheckBox;
    CheckBox workCheckBox;
    CheckBox foodCheckBox;

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

        editTextExpense = (EditText)findViewById(R.id.moneySpent);

        personalCheckBox = (CheckBox)findViewById(R.id.personalCheckBox);
        commuteCheckBox = (CheckBox)findViewById(R.id.commuteCheckBox);
        billsCheckBox = (CheckBox)findViewById(R.id.billsCheckBox);
        funCheckBox = (CheckBox)findViewById(R.id.funCheckBox);
        workCheckBox = (CheckBox)findViewById(R.id.workCheckBox);
        foodCheckBox = (CheckBox)findViewById(R.id.foodCheckBox);


        backToMainMenu();
    }

    public void backToMainMenu() {


        final Button expenseButton = (Button)findViewById(R.id.addExpenseSecondScreen);
        expenseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (personalCheckBox.isChecked()) {
                    selectedCheckBox = personalCheckBox.getText().toString();
                }
                if (commuteCheckBox.isChecked()) {
                    selectedCheckBox = commuteCheckBox.getText().toString();
                }
                if (billsCheckBox.isChecked()) {
                    selectedCheckBox = billsCheckBox.getText().toString();
                }
                if (funCheckBox.isChecked()) {
                    selectedCheckBox = funCheckBox.getText().toString();
                }
                if (workCheckBox.isChecked()) {
                    selectedCheckBox = workCheckBox.getText().toString();
                }
                if (foodCheckBox.isChecked()) {
                    selectedCheckBox = foodCheckBox.getText().toString();
                }


                float expense = Float.parseFloat(editTextExpense.getText().toString());
                Intent intent = getIntent();
                intent.putExtra("expense", expense);
                intent.putExtra("checkBoxText", selectedCheckBox);
                setResult(RESULT_OK, intent);

                finish();



            }
        });
    }


}

Solution

  • I am not sure what you are expecting here but onActivityResult is used to get the result from starting an Activity using startActivityForResult and will only be called in the activity which called startActivityForResult.

    Since activities are only active one at a time (unless you are targeting the N preview) you would have to pass the result data using the start intent to the other activity.