0

IN my first activity I have this code (that is a onCLick event of a Button):

  public void uno(View v)
   {
    Partita p = new Partita();
    p.cont = 1;
    p.puntiCategoriaG1 = 20;
    p.puntiCategoriaPC = 15;

    Intent i = new Intent(this, Partita.class);
    startActivity(i);
    finish();
  }

And in the second class/activity I set them:

public class Partita extends Activity {
   public int cont;
   public int puntiCategoriaG1;
   public int puntiCategoriaPC;
   @Override
   public void onCreate(....

But when I go to use them in the code the have the value 0. How can I solve?

rene
  • 37,946
  • 78
  • 99
  • 132
Mattia
  • 179
  • 9

3 Answers3

3

You should add a parameter into Intent

public void uno(View v) {
    Intent i = new Intent(this, Partita.class);
    i.putExtra("cont", 1);
    startActivity(i);
}

And now in your second Activity you can retrieve it:

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    int cont = getIntent().getExtras().getInt("cont", 0);
    Log.d("TAG", cont);
}

You have not to create an instance of Partita activity before launching.

Partita p = new Partita();
p.cont = 1;
p.puntiCategoriaG1 = 20;
p.puntiCategoriaPC = 15;

When you call startActivity(i); you don't start this created object. Android builds and shows the Activity itself. To pass some parameters into the second Activity, add them into Intent as I described above.

yital9
  • 6,038
  • 13
  • 37
  • 51
  • But i need to decide the value of the variable in the 1st activity and in the second only take it. In this way i set the value in the second activity @yital – Mattia Mar 12 '16 at 19:14
  • No, you set the value in "public void uno(View v)" method in the first Activity: i.putExtra("cont", p.cont). At this point cont = getIntent().getExtras().getInt("cont ", 0); you read it. "0" here is the default value, if you didn't set any value to Intent with such key – yital9 Mar 12 '16 at 19:17
  • i put i.putExtra("cont",cont); with cont =1 in the first activity and i put public int cont = getIntent().getExtras().getInt("cont",0); in the second activity.....but when i launch the app it closes when i click – Mattia Mar 12 '16 at 19:25
  • did you add your second Activity into manifest? – yital9 Mar 12 '16 at 19:26
  • i think no...ii need to insert? --edit--- is this? – Mattia Mar 12 '16 at 19:28
  • yes, right, in block – yital9 Mar 12 '16 at 19:31
0

see when you start activity you just pass the .class I.e the object of Class.java associated with Partita.java. You should not create the object of Activity. You just start it and its the Android framework which takes care of creating the object and calling the lifecycle methods of an activity. You use Objecy of Intent.java to start an activity. For passing data from one activity to another I suggest you to study about putExtra method in Intent.java. Also study about as how to wrap data in Bundel and then how to pass it to object of Intent which is used to start new activity.

nits.kk
  • 4,781
  • 4
  • 24
  • 48
0

Android Activities do not work like normal classes. ie: You do NOT specifically create the instance. You simply create an Intent with the intention to start a specific activity. The operating system takes care of initializing the instance of the new activity.

Passing data between activities is different than passing data within classes. Please check this post to see how it's done.

Community
  • 1
  • 1
Ruchira Randana
  • 3,525
  • 1
  • 24
  • 23