-4

I want to pass my API response to another activity . I am using intent but getting null in the next activity . My code is MainPage.java

 public void onResponse(String response) {

                System.out.println("output -- "+response);
                members = response;
                Intent intn = new Intent(MainPage.this,Calculation.class);
                intn.putExtra("MEMBERS",members);

            }

I am getting the correct response .

Calculation.java

Intent intn = new Intent();
        members = intn.getStringExtra("MEMBERS");
        System.out.println("dmkmdk"+members);
        //no_of_members = Integer.parseInt(members);

I also want to parse it in integer form .

user6914996
  • 41
  • 1
  • 6
  • You are missing `startActivity` in first snippet . and Use `getIntent().getStringExtra("MEMBERS");` in next one . – ADM May 26 '18 at 11:41
  • but I don't want to start a new activity . I just want to pass the value – user6914996 May 26 '18 at 11:42
  • 1
    Then you should edit your question . In this case your code is useless . Use a `Broadcastreceiver` instead or `startActivityForResult`. – ADM May 26 '18 at 11:43
  • we just have to pass the value from one activity to another bu we are getting null we dont have to start the activity we just have to use that variable "member" in other activity...... – user6914996 May 26 '18 at 12:01

3 Answers3

0

you should get extra like this, don't initiate an intent object

Bundle extras = getIntent().getExtras();
String value1 = extras.getString("MEMBERS");
masoud vali
  • 1,528
  • 2
  • 18
  • 29
0

Try to save data in SharedPreference instead Intent.

SharedPreferences pref = 
getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();

For Storing data:-

editor.putString("MEMBERS",members);
editor.apply();

For retriving data:-

editor.getString("MEMBERS", null); // getting String
Mr. Roshan
  • 1,777
  • 13
  • 33
  • we just have to pass the value from one activity to another but we are getting null we just have to use that variable "member" in other activity.without using shared preference is there any other solution – user6914996 May 26 '18 at 12:02
  • make a one class in which create a variable and when your onResume returns result assign it to that variable and access on another activity. – Mr. Roshan May 26 '18 at 12:06
  • can you make it more clear plz ? can u please give one demo example? – user6914996 May 26 '18 at 12:08
  • refer this:-https://stackoverflow.com/questions/4878159/whats-the-best-way-to-share-data-between-activities – Mr. Roshan May 26 '18 at 12:15
0

Please try this

To Transfer data in next activity

public void onResponse(String response) {

                    System.out.println("output -- "+response);
                    members = response;
                    Intent intn = new Intent(MainPage.this,Calculation.class);
                    intn.putExtra("MEMBERS",members);
    startActivity(intn)

                }

In New activity where you want to get data try this line

String resFromLast = getIntent().getExtras().getString("MEMBERS");
Log.e("Response From last activity is :" ,resFromLast);

MEMBERS is that key you passed in last activity with intent.

Hope it works.

Sandeep Parish
  • 1,888
  • 2
  • 9
  • 20