App crashes after calling intent variable - android

I'm passing info from one activity to another but there is a statement that isn't allowing it to happen, I'm unsure why though, could someone help, please?
Activity1(Kotlin based)
val spinnerMod = findViewById<View>(R.id.spinner_searchMod) as Spinner
val intent = Intent(this#AddModActivity, EditImportActivity::class.java)
intent.putExtra("module", listMod[spinnerMod.selectedItemPosition].ID)
startActivity(intent)
Activity2(Java based)
Intent intent = getIntent();
String module = intent.getParcelableExtra("module").toString();
The line causing the issue is String module = intent.getParcelableExtra("module").toString();

There is no need to use getParcelableExtra. You can directly use
Intent intent = getIntent();
String module = intent.getStringExtra("module");

Related

intent.getStringExtra() returns null

Im trying to send data class object to another activity with intent but it returned null so i tried sending simple string but it still returns null, i couldn't find what is the problem. Here is my code
code in LoginScreen.kt
val intentUser = Intent(this#LoginScreen,HomeActivity::class.java)
val string = "intent"
intentUser.putExtra("intent",string)
startActivity(intentUser)
finish()
and code in my HomeActivity
val intentUser = Intent()
var string = intentUser.getStringExtra("intent")
Log.e("Intent: ",string.toString())
result
2022-02-23 14:25:09.139 11365-11365/com.scibilisim.d_forceandroid E/Intent:: null
Please try this in your LoginScreen.kt:
val intentUser = Intent(this#LoginScreen,HomeActivity::class.java)
val string = "intent"
intentUser.putString("intent",string)
startActivity(intentUser)
this.finish()
and in your HomeActivity:
val string = intent!!.getStringExtra("intent")
The error is that you are instantiating a new Intent object.
in HomeActivity you are initializing intent as
intent = Intent()
This actually initializes the intent with a new intent object. That is not desired behavior, to achieve desired behavior use getIntent() instead of Intent().

Intent data is lost in Activity

Yeah, I know that is so simple to pass data with an intent but suddendly not works. Extra data is always null.
Someone can tell me why... because I can't see any error.
I can debug the code, and when is in startActivity(intent) I can see the intent has extra data set correctly. When I stop the code in getIntent().getExtras() data extra is lost
Activity A
Intent intent = new Intent(mActivity, ScreenActivity.class);
intent.putExtra(ArgumentsNamesConfig.IS_PUSH_NOTIFICATION, true);
intent.putExtra(ArgumentsNamesConfig.ARG_ARTICLE, article);
intent.putExtra(MyFirebaseMessagingService.IS_GEO_NOTIFICATION, isGeoNotification);
mActivity.startActivity(intent);
ScreenActivity.class ( get in onCreate method)
Intent intent = getIntent();
pushNotificationProductId = intent.getBooleanExtra(ArgumentsNamesConfig.IS_PUSH_NOTIFICATION);
isGeoNotification = intent.getBooleanExtra(MyFirebaseMessagingService.IS_GEO_NOTIFICATION, false);
mArticle = intent.getParcelable(ArgumentsNamesConfig.ARG_ARTICLE);
intent.putExtra(ArgumentsNamesConfig.IS_PUSH_NOTIFICATION, true); is boolean and u are trying to get it as STRING(intent.getStringExtra(ArgumentsNamesConfig.IS_PUSH_NOTIFICATION)
try changing tour code to this
Intent intent = getIntent();
pushNotificationProductId = intent.getBooleanExtra(ArgumentsNamesConfig.IS_PUSH_NOTIFICATION);
isGeoNotification = intent.getBooleanExtra(MyFirebaseMessagingService.IS_GEO_NOTIFICATION, false);
mArticle = intent.getParcelable(ArgumentsNamesConfig.ARG_ARTICLE);

How to peek into Intent Extras in Android

I have an application that starts Activity with extras. I would like to see what extras are inside Intent. Both applications (calling and called) are not written by me.
Send the data first:
Intent i = new Intent(YourActivity.class);
i.putExtra("parameter", yourData)
startActivity(i);
To get the data :
Intent i = getIntent();
String data = i.getStringExtra("parameter"); //you can do for integer, etc

How does putExtra() works?

Can you tell me how putExtra() works? How and where does it stores the data passed to it ? I am still confused how the data inside it works and how it is passed from one activity to other activity? How its structure is organised? Or the values stored on the inside of the Intent class ?
Imagine there is class A.
it has one static string Example.I want to fill this string in next activity so I call activityforresult()
Now there is class B.I filled textbox with id text_entered. and I put the code
Intent text = new Intent();
test.putExtra(A.Example, text_entered);
Now where will be text saved, in A example or in intent test?
Activity1.class
Intent intent = new Intent(getApplicationContext(), Activity2.class);
intent.putExtra("EXTRA_NAME", 4);//Int
intent.putExtra("EXTRA_NAME", "String_Values");//String
Activity2.class
String stringValue = getIntent().getStringExtra("EXTRA_NAME");//String extra
int iValue = getIntent().getIntExtra("EXTRA_NAME", 0);//defaultValue = 0
Your text will be carried along with the intent to the other activity as described by #NamHoang in his answer. The A.Example (should be a string) in your case is just a key that is used to refer the value present in the intent.
I hope you know that it is the key-value pair that is being sent along with the intent.
A.class
static String Example = "this is a example string";
private void JumpToB(){
Intent intent = new Intent();
intent.putExtra("key_data", Example);
intent.setClass(this,B.class);
startActivity(intent);
}
B.class
protected void onCreate(Bundle savedInstanceState) {
String example = getIntent().getStringExtra("key_data");
text_entered.setText(example);
}
If you want to pass a Model, a good way is to transform the model to json.

Exception while passing extras through intent

I want to pass a string from 1 activity to another, although I have taken reference from many accepted answers from other threads, I am facing problem that I am not able to debug. When I comment extras.putString as shown in code below, Toast message shows the correct address which means value is being set properly and code works fine but when I use extras.putString(), I get NullPointerException and application closes due to exception. There are many \n characters in my address string. Infact even if I use extras.putString("userAddress", "test") I get NullPointerException
Here is my Main Activity from which I want to call FBShare Activity:
Intent mIntent = new Intent(this, FBShare.class);
Bundle extras = mIntent.getExtras();
String currentAddress = getCurrentAddress(ourLocation);
Toast.makeText(getBaseContext(), getCurrentAddress(ourLocation), Toast.LENGTH_SHORT).show();
extras.putString("userAddress", currentAddress);
startActivity(mIntent);
And in FBShare Activity I am trying to fetch values as follows
strAddress = getIntent().getExtras().getString("userAddress");
Here is one thread which is doing similar thing.
try my code
Intent mIntent = new Intent(this, FBShare.class);
Bundle extras = new Bundle();
String currentAddress = getCurrentAddress(ourLocation);
Toast.makeText(getBaseContext(), getCurrentAddress(ourLocation), Toast.LENGTH_SHORT).show();
extras.putString("userAddress", currentAddress);
mIntent.putExtras(extras);
startActivity(mIntent);
hope this will work.
Try directly putting extra on the intent:
mIntent.putExtra("Key", "Value")
Also, you retrieve the extra using
Intene t = getIntent();
String k="key";
if (t.hasExtra(k)) {
s = t.getStringExtra(k);
...
}
The are get/put for many var types
Intent mIntent = new Intent(this, FBShare.class);
String currentAddress = getCurrentAddress(ourLocation);
Toast.makeText(getBaseContext(), getCurrentAddress(ourLocation), Toast.LENGTH_SHORT).show();
mIntent.putString("userAddress", currentAddress);
startActivity(mIntent);
Bundle is not need in your case because it seems that you are only pasing a string you can use the above code..

Categories

Resources