Java how to send int value from one actvi to another in android

Java how to send int value from one actvi to another in android

To send an integer value from one activity to another in Android, you can use an intent and put the integer as an extra. Here is an example:

  1. In the first activity, create an intent and put the integer as an extra:
refer to:‮iruttual‬.com
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("key", 123);
startActivity(intent);
  1. In the second activity, get the integer extra from the intent:
int value = getIntent().getIntExtra("key", 0);

The second parameter of the getIntExtra method is the default value, which will be used if the extra is not found.

You can also use other types of data, such as strings or arrays, by using the corresponding putExtra and getExtra methods. For example, to send a string, you can use putExtra("key", "value") and to retrieve it, you can use getStringExtra("key").

Created Time:2017-11-03 23:27:11  Author:lautturi