0

I need to transfer User and int between 2 activities, this happens when clicked on item in listview, Activity 1:

Intent intent = new Intent(GameActivity.this,ServerActivity.class);
intent.putExtra("serverNum",position);
intent.putExtra("currentUser",currentUser);
Log.d("position", position + "");
startActivity(intent);
finish();

the position is by defult the number of the item that the user clicked on, The log.d shows the currect position indeed, but when I try to get that Int in activity 2 its always 0.

In activity 2:

Log.d("Server Number",getIntent().getExtras().getInt("serverNum")+"");

this log.d always shows 0. the user transfers well though.

EDIT

I found out it has something to do with android:launchMode="singleTask" in the manifest, for some reason the activity opens twice, one opens well and one for some reason opens with the extra "serverNum" equals to 0, any suggestions on how to fix this?

  • Sure you don't have a typo in the name? Its considered best to use a constant to avoid that possibility. – Gabe Sechan Jan 15 '17 at 17:05
  • pretty sure for couple of reasons, 1 I dont get null, the log says "D/Server Number: 0", and I checked like 10 times its not a typo by coping and pasting – Netanel Malichi Jan 15 '17 at 17:08

3 Answers3

1

Try this in your Activity 2.

int id=getIntent().getIntExtra("serverNum", 0);

Log.d("ServerNumber",id+"");
John Joe
  • 12,412
  • 16
  • 70
  • 135
0

Try overriding method and check value there as well

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
int id=intent.getIntExtra("serverNum", 0);
Log.d("ServerNumber",id+"");
setIntent(intent);

}
amodkanthe
  • 4,345
  • 6
  • 36
  • 77
0

You should get rid of android:launchMode="singleTask". The reason is that this creates only one instance of the activity. Once you go back to it, it resets the extra to 0. Here you can read up on launch types: https://inthecheesefactory.com/blog/understand-android-activity-launchmode/en

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156