How to Use Putextra() and Getextra() For String Data

How to use putExtra() and getExtra() for string data

Use this to "put" the file...

Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
String strName = null;
i.putExtra("STRING_I_NEED", strName);

Then, to retrieve the value try something like:

String newString;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("STRING_I_NEED");
}
} else {
newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}

How do you use the putExtra() and getExtras() code to get an int value?

This is how you should pass data

Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
intent.putExtra("isMeasurement", 2);
startActivity(intent);

Inside your onCreate

This is how you can retrieve data in MainActivity

int isMeasurement = getIntent().getIntExtra("isMeasurement",0); // Here 0 is a default value.It could be anything acc to your requirement

Intent.putExtra - How to send a string to the next activity?

First Activity :

Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("Variable Name",string_to_be_sent);
startActivity(intent);

Second Activity :

//Receiving data inside onCreate() method of Second Activity
String value = getIntent().getStringExtra("Variable Name");

Send data with putExtra and getExtra

You are not using the position variable anywhere in your code. The code line

File pdfFile = new File(Environment.getExternalStorageDirectory(),"PdfFile.pdf");

always gets the same pdf file, therefore you should appropriately pass the position parameter to this line to open the desired file.

How do I get extra data from intent on Android?

First, get the intent which has started your activity using the getIntent() method:

Intent intent = getIntent();

If your extra data is represented as strings, then you can use intent.getStringExtra(String name) method. In your case:

String id = intent.getStringExtra("id");
String name = intent.getStringExtra("name");

Intents getExtra()/putExtra in Android Api level 30 not working

I managed to find a fix for this issue. It seems the newer versions of Android have a separate permission layer for more secure intent action as per this link.

Basically I added this line to both applications’ AndroidManifest.xml files (Android Studio app and Unity app):

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" tools:ignore="QueryAllPackagesPermission" />

Get string extra from activity Kotlin

Answer found, in the next activity, you have to do this to get the string:

val ss:String = intent.getStringExtra("samplename").toString()


Related Topics



Leave a reply



Submit