getIntExtra always returns the default value
I am trying to pass an integer between activities using an intent. The source activity makes the calls info.id is the selected item from a ListView.
Intent intent = new Intent(myActivity.this, newClass.class);
intent.putExtra("selectedItem", info.id);
this.startActivity(intent);
The target activity retrieves the intent using getIntent then calls
int iSelectedItem = intent.getIntExtra("selectedItem", -1);
iSelectedItem is always -1 instead of the value passed to putExtra. Can someone tell me what I am doing wrong or do I misunderstand the use of intents?
Asked by: Alford635 | Posted: 25-01-2022
Answer 1
The problem is that info.id will be a 'long' and is not converting to an 'int'. Try
long iSelectedItem = intent.getLongExtra("selectedItem", -1)
Answered by: Ada784 | Posted: 26-02-2022
Answer 2
I don't find putIntExtra()
method. So I ended up with following:
intent.putExtra("jobId", 1);
and
Integer.parseInt(getIntent().getExtras().get("jobId").ToString());
Use try and catch to handle Exceptions.
UPDATE
Later I found that I was passing jobId as a string in putExtra()
method, therefore getIntExtra()
was always returning the default value.
So @Grant is correct. You must pass an Integer value in putExtra()
method to use getIntExtra()
method.
Answer 3
I had this problem and it was a simple thing.
Check if you're using onActivityResult ... than, you don't have to use getIntent() to get the extras, you have to use the intent you pass as parameter.
In your case should be something like this:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode == RESULT_OK) {
int iSelectedItem = intent.getIntExtra("selectedItem", -1);
Log.d("DEBUG", "check iSelectedItem = " + iSelectedItem);
}
}
observe that I'm not using getIntent(), but the argument intent.
(PS: if you're calling a lot of activities expecting results, is better to check if the intent is != null)
I hope it helps.
Answered by: Rubie725 | Posted: 26-02-2022Answer 4
Easy:
Bundle bundle = getIntent().getExtras();
int iSelectedItem = bundle.getInt("selectedItem", -1);
Now, if you're using StartActivityForResult and you want to return some data, from the child activity, remind that you have to use onActivityResult
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode == RESULT_OK) {
int iSelectedItem = intent.getExtras.getInt("selectedItem", -1);
Log.d("DEBUG", "check iSelectedItem = " + iSelectedItem);
}
}
Remember, is the same way like you get extras from other Activity, just using bundle.getInt, in this example, getExtras return a bundle, so, across this bundle, you can get any data that you have sent from the resultIntent.
Answered by: Joyce866 | Posted: 26-02-2022Answer 5
In my case, it was because I created the object with mId member variable declared as string
public class Application {
private String mId;
....
}
intent.putExtra("id", myApplication.getId());
and as such, the Extra is passed as string. simply change your member variable to int, you get the idea ;)
Answered by: Aston471 | Posted: 26-02-2022Answer 6
int sub_menu_id = 0;
int question_part = 0;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if (extras == null) {
sub_menu_id = -1;
question_part = -1;
} else {
sub_menu_id = extras.getInt("sub_menu_id");
question_part = extras.getInt("question_part");
}
}
Log.d("DREG", "sub_menu_id: " + sub_menu_id + "\nquestion_part: " + question_part);
Answered by: Michael809 | Posted: 26-02-2022
Answer 7
In my case, I converted the value inside putExtra to String from Int and Long and while receiving, received it as String only. Can't figure out why but Integer, Long were showing defaultValues
while after converting to String it works.
Example :
intent.putExtra("code", response.data?.code.toString())
val code = intent.getStringExtra("code")
Answered by: Sarah728 | Posted: 26-02-2022
Similar questions
java - How to properly use getIntExtra?
I'am trying to pass an integer between two activities.
Activity1:
Intent intent = new Intent(this, Activity2.class);
int num1=40;
intent.putExtra("num1", num1);
startActivity(intent);
Activity2:
Intent intent = getIntent();
int num = intent.getIntExtra("num1", 1);
TextView tv = (TextView) findViewById(R.id.tb_01);
tv.setText(num);
When I start the ...
android - getIntExtra always returns default current posts not working either
When passing data between activity SongDetails and MainActivity getIntExtra always returns default value. Please see code below.
First Activity
Log.d("URL", "isIntentDriven = TRUE");
Intent intent = new Intent(SongDetails.this, MainActivity.class);
Log.d("URL", " " + sid);
intent.putExtra("sid", sid);
startActivity(inten...
java - android getIntExtra what is the value for?
when you want to get an integer from another activity and write for example
Intent getData = new Intent();
int test = getData.getIntExtra("ship1", 0);
planets += test;
Why does my planets variable add the value from the getIntExtra instead of the actual integer from the other activity. And can someone also explain what that value is for and how to add the actual integer from the other acti...
android - Funny things happening with putExtra and getIntExtra
I'm developing a game, on my menu screen I have a series of buttons which take the player to different levels of the game. I've used putExtra and getIntExtra to take an Int from the menu activity to the game activity, this then sets the play level.
This is all working nicely, the problem I am having is when using the "back" button to go back to the menu activity you have to hit it twice to get to the menu instead ...
Android intent set getIntExtra as global variable
I have three activities, Main, SecondActivity and ThirdActivity. I want to send int variable from Main to SecondActivity, and after that, send int(number) from Main(which I got) and SecondActivity to ThirdActivity.
MainActivity:
String Score = 0;
public void onNextClick(View view){
Score++;
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("Answer", Sco...
kotlin - getIntExtra in ViewModel android
I have been trying to convert this small project to MVVM, where i switch the json file based on the previous activity, but i cannot use this intent.getIntExtra in ViewModel. how do i achieve that, the code given below is the one i wrote in the activity class, but i can't do
intent.getIntExtra in viemodel. I am new to MVVM i want to use the ame function in viewModel class
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android