-1

ANSWERED

I am trying to send and int to another activity.There is a button in first act. and it opens second act. and takes int from here to other page.But i cant start two of them at one time. Here is my first activity :

public class ana_ekran extends AppCompatActivity {

    public TextView ana_ekran_kule;
    public TextView ana_ekran_can;
    public  int ana_ekran_can_int=30;


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



    ana_ekran_kule=(TextView) findViewById(R.id.textView);
    ana_ekran_can=(TextView) findViewById(R.id.textView2);
    ana_ekran_can.setText(ana_ekran_can_int+" CAN");

    }
public void devam (View v){
    Intent i = new Intent(getApplicationContext(),fight_1.class);
    i.putExtra("deger",  ana_ekran_can_int);
    startActivity(i);
    startActivity(new Intent(this,fight_1.class));

}
}

and this is second:

public class fight_1 extends AppCompatActivity {

    public TextView fight_1_can;
    public int fight_1_can_int;

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

        fight_1_can=(TextView) findViewById(R.id.textView3);

        int i = getIntent().getIntExtra("deger",-1);
        fight_1_can_int=i;
        fight_1_can.setText(fight_1_can_int+"");

    }

}
  • remove this ` startActivity(new Intent(this,fight_1.class));` why redundant? – ZeroOne Apr 11 '18 at 17:13
  • Possible duplicate of [How to send an object from one Android Activity to another using Intents?](https://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents) – Chisko Apr 11 '18 at 17:56

1 Answers1

2

This startActivity(i); will do the same thing as next line but it will carry the data as well whereas startActivity(new Intent(this,fight_1.class)); will only start the other instance of fight_1 activity so

public void devam (View v){
    Intent i = new Intent(getApplicationContext(),fight_1.class);
    i.putExtra("deger", ana_ekran_can_int);
    startActivity(i);
    // start fight_1 again without any data so not required
    //startActivity(new Intent(this,fight_1.class));    
}
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68