-1

I have 2 activities in my app. I'm sending the integer named balance from MainActivity to UpgradesActivity but I'm not sure how to send it back. Could anyone help me with that? Here's the code:

        //Send balance to UpgradesActivity
        Intent intent = new Intent(MainActivity.this, UpgradesActivity.class);
        intent.putExtra("key_int", balance);
        startActivity(intent);

        //Receive balance from MainActivity
       Intent mIntent = getIntent();
       balance = mIntent.getIntExtra("key_int", 0);
Camper1233
  • 15
  • 3
  • I am sorry about that, i searched but i didn't find an answer, i will delete the question soon, do not worry. – Camper1233 May 17 '16 at 22:01
  • I take no personal offense that you've posted a duplicate, but this is a quite common question, so it seems to me you hadn't done your research. – Vucko May 17 '16 at 22:01

2 Answers2

0

Check this Android Developer doc to learn about starting Activities for Result :-) This way you will be able to start an Activity stating you want to get something back, do some work in the new Activity and then get the result in onActivityResult() of the first one. It is really easy, just go through the doc :-)

https://developer.android.com/training/basics/intents/result.html

Kelevandos
  • 7,024
  • 2
  • 29
  • 46
  • Could you please just give me the code? – Camper1233 May 17 '16 at 19:44
  • @Camper1233 Take a look at my response, The code is in the question. Part of programming requires you to do some work yourself though. – Errol Green May 17 '16 at 19:44
  • No, because I don't know anything about your app. Also, StackOverflow is about helping you understand how to solve your problems, not about writing your program for you :-) – Kelevandos May 17 '16 at 19:45
0

You can use onActivityResult(). Please take a look at this answer.

https://stackoverflow.com/a/947560/4739608

Community
  • 1
  • 1
Errol Green
  • 1,367
  • 5
  • 19
  • 32