0

I am writing my first Android app.

I want to input 3 integers in the activity A, and do the same in activities B and C. Then I want to calculate something with all 9 integers and display the result in activity D.

How can I get the integers from the first 3 activities into the last activity?

EDIT: I have tried it for two Activity..the integer in input from MainActivity and the output is print in TextView in Activity 2...But the answer in Activity 2 is always is zero.

Code is :-

public class MainActivity extends ActionBarActivity {

int int1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button b = (Button) findViewById(R.id.bt1);
    EditText et = (EditText) findViewById(R.id.edT1);
    try{
    int1 = Integer.parseInt(et.getText().toString());
    } catch (NumberFormatException e)
    {
        e.printStackTrace();
    }
    b.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(MainActivity.this,Activity2.class);
            i.putExtra("val1",int1);
            startActivity(i);

        }
    });
}

}

And for Activity 2 is :-

public class Activity2 extends Activity {

int value;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity2xml);
    TextView t2 = (TextView) findViewById(R.id.tt2);
    Intent intent = getIntent();
    int a = intent.getIntExtra("val1",0);
    t2.setText("Bingo "+String.valueOf(a));

}

}

  • Welcome to Stack Overflow! I reworded your question in more approachable English, so more people will read it. If you have tried something already, please post the code from your previous try and an explanation of what didn't work. Good luck! – tbodt Mar 21 '15 at 22:20

3 Answers3

0

You can send primitive data type with putExtra method. Simply using it like this.

//in activity A 
Intent myIntent = new Intent(this,ActivityB.class);
myIntent.putExtra("int1", 3);
myIntent.putExtra("int2", 5);
startActivity(myIntent);

//you can get this values in Activty B

Bundle extras = getIntent().getExtras();
int int1= extras.getInt("int1");
int int2 = extras.getInt("int2");

and you can go on like this for ActivityB to AcitivtyC and ActivityC to ActivityD.

Cüneyt
  • 2,565
  • 25
  • 31
0

You have to pass the values in the Intent from activity to activity until Activity D.

class ActivityA extends Activity {
   ...
   private void startActivityB(){
      Intent i = new Intent(this, ActivityB.class);
      i.putExtra("val1", 1);
      i.putExtra("val2", 1);
      i.putExtra("val3", 1);
      startActivity(i);
   }
}

In ActivityB you have to read the value passed from ActivityA and pass these values to ActivityC with the new ones:

class ActivityB extends Activity {
   ...
   private void startActivityC(){

      int val1 = getIntent().getIntExtra("val1", 0);
      int val2 = getIntent().getIntExtra("val2", 0);
      int val3 = getIntent().getIntExtra("val3", 0);

      Intent i = new Intent(this, ActivityC.class);
      i.putExtra("val1", val1);
      i.putExtra("val2", val2);
      i.putExtra("val3", val3);

      i.putExtra("val4", 5);
      i.putExtra("val5", 6);
      i.putExtra("val6", 7);

      startActivity(i);
   }
}

Do the same for ActivityC and in ActivityD read the values out. I hope you get the idea.

Btw. an optimization would be to pass the sum from Activity to Activity instead of the single values, if you know that Activity D should displays the sum.

sockeqwe
  • 15,574
  • 24
  • 88
  • 144
  • In Activity A the value of the integer are taken from the user by ExitText...so in i.putExtra("val1", 1); what should i write instead of 1 here....and thanks for the reply..:) – Himanshu Malhotra Mar 22 '15 at 09:16
  • I have edited the question...@sockeqwe please take a look....Then answer i.e the value of the integer in Activity2 is always printed as 0 – Himanshu Malhotra Mar 22 '15 at 10:41
  • @HimanshuMalhotra There are two methods and you mixed up both. `intent.putExtra("val1", int)` must be read with `intent.getIntExtra("val1")`. There is a second way to put data in a `Bundle` for an Intent: `Bundle b = new Bundle(); b.putInt("val1", int); intent.putExtra(b);` Then you have to use `intent.getExtra().getInt("val1");` See http://stackoverflow.com/questions/15243798/advantages-of-using-bundle-instead-of-direct-intent-putextra-in-android – sockeqwe Mar 22 '15 at 10:53
  • In the first method i am getting an error in intent.getIntExtra("val1") ....that is The method getIntExtra(String, int) in the type Intent is not applicable for the arguments (String) – Himanshu Malhotra Mar 22 '15 at 13:11
  • Come on man, read the compiler error message and have a look at the java docs. There is a second parameter needed, the default value which is returned if the intent does not contain the data with the key "val1". So your call should be `intent.getIntExtra("val1", 0); `. http://developer.android.com/reference/android/content/Intent.html#getIntExtra(java.lang.String, int) Btw. you should accept the correct answer – sockeqwe Mar 22 '15 at 13:15
  • i am just learning sockeqwe....and thank you so much...for your help :) and i already tried that....("val1",0)...and then t2.setText(" "+a) where int a = intent.getIntExtra("val1",0) ...but the answer is still coming 0 – Himanshu Malhotra Mar 22 '15 at 13:20
  • @HimanshuMalhotra I have setup a sample project that works: https://github.com/sockeqwe/PassingIntentTest – sockeqwe Mar 22 '15 at 13:37
  • you have written i.putExtra("val1", 3); and i am taking the value from the user and then passing the value..thus i am writing i.putExtra("val1",int1); ( see the question above.....i have again edited the code....) is that why i am getting the answer as zero....??? also if i put a constant integer such 3 then i am getting the correct output.....thus i think that the problem is in taking the input from the user....please take a look at the code again – Himanshu Malhotra Mar 22 '15 at 14:08
  • Bingo......I have figured it out...i was not taking input from edittext in onClick (view v) method....oh...man....it was such a foolish mistake...Thank you so much @sockeqwe...i have to say...you have helped me to create my first android app...Thank you so much..:) – Himanshu Malhotra Mar 23 '15 at 18:07
  • i don't have enough reputations to vote up...otherwise i would have definitely vote this answer (y) :D – Himanshu Malhotra Mar 23 '15 at 18:09
-1

Store the values that you input in Activity A,B,C in a SharedPreference object. Retrieve all the values from that object in Activity D.

Reference of how to use sharedpreferences can be found at - http://developer.android.com/training/basics/data-storage/shared-preferences.html

TechnoBlahble
  • 633
  • 5
  • 14