0

I just started learning android development and thought I'd make a bill splitting app. However, I just started learning a multipage activitiy however I have no clue how to get data from other activities.

So what I'm trying to do is take a calculated amount of money from the Main Activity and display a summary on the second page.

int friendsInit = 1;
float collectAmount = splitCalc();

/* Friends increment/decrement functions */


public void friendIncrement(View view) {
    friendsInit = friendsInit + 1;
    friendsUpdate(friendsInit);
}

public void friendsDecrement(View view) {
    if (friendsInit > 1) {
        friendsInit = friendsInit - 1;
    }
    friendsUpdate(friendsInit);
}

/* Split calculations and update */

/*Rounding procedure*/

public static float round(float d, int decimalPlace) {
    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd.floatValue();
}

public float splitCalc() {
    EditText amountText = (EditText) findViewById(R.id.amount);
    float number = Float.valueOf(amountText.getText().toString());
    float collectAmount = round(number / (friendsInit + 1), 2);
    return collectAmount;

}

So I want to use the collectAmount variable in the second activity. I did a little searching around and I found that you use.

float collectAmount = MainActivity.collectAmount;

But it gives me the error, "non-static field cannot be referenced from a static context". I'm not sure what this means or how to remedy it.

Any guidance on how to fix this issue would be appreciated or if there is a better way of storing data in your application.

fynmnx
  • 571
  • 6
  • 29
  • Possible duplicate of [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) – Ivan Wooll Nov 03 '18 at 16:31

1 Answers1

3

You can pass variable through Intent. For example like this:

In your MainActivity

public void startActivity() {
    Intent intent = new Intent(this, SecondActivity.class);
    intent.putExtra("example-extra", 5);
    startActivity(intent);
}

In your second activity you can get your variable like this:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    int example_extra = getIntent().getIntExtra("example_extra", 0);
}
Jan Maděra
  • 521
  • 3
  • 5
  • 17
  • In the second activity code, what does the second parameter 0, represent? Also, is "example-extra" the variable name or do I have to create it separately? – fynmnx Nov 03 '18 at 16:42
  • It represent the default value. So if your are calling for Extra which does not exists it will return 0 in this case. – Jan Maděra Nov 03 '18 at 17:14
  • Oh okay, can you omit it if you don't want it to return 0? – fynmnx Nov 03 '18 at 17:52
  • You can't. Because in Java int is not nullable so you have to define default value if something went wrong. But can return something else instead of 0. – Jan Maděra Nov 03 '18 at 18:12