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().
Related
I have an edit text from where I am taking the text and sending it to MainActivity using putExtra()
but when the getStringExtra() is returning null.
SecondActivity:-
val intent = Intent(this, MainActivity::class.java)
if(editWordView.text.isNotEmpty()){
val word = editWordView.text.toString()
intent.putExtra("Word", word)
startActivity(intent)
}
In first log it is showing null and last line is not executing as the word is null
MainActivity:-
val intent = Intent()
val word = intent.getStringExtra("Word")
Log.d(TAG, "MainActivity: $word")
word?.let {
viewModel.insert(Word(word))
Log.d(TAG, "onCreate: Inserted $word")
}
Delete:
val intent = Intent()
You are creating a new, empty Intent. That will not contain any extras. Instead, you need to get the Intent from the activity:
via getIntent() for the Intent used to create the activity
in onNewIntent() for any Intent used to bring an already-running instance of this activity back to the foreground
To get value in the Second activity you can also try this, it works perfect for me!
val word = intent.extras?.getString("Word")
I am trying to get a string from 1 activity to another, and then get that string from the second activity to the Fragment inside of it. I am getting null return from the fragment. This is the code for each of the following.
Activity 1
findViewById<Button>(R.id.button).setOnClickListener {
val intent = Intent(this, MainActivity::class.java)
intent.putExtra("IPValT", IPVal)
startActivity(intent)
Activity 2
var IPVal2: String = intent.getStringExtra("IPValT").toString()
camBut.setOnClickListener {
val intent = Intent(this, Video::class.java)
intent.putExtra("IPValT2", IPVal2)
startActivity(intent)
Fragment
val ipcomm: String = mainAct.intent.getStringExtra("IPVal2").toString()
What am I doing wrong? Any help is greatly appreciated. Thanks!
Your key value is mismachted .In your fragment add IPValT2 instead of IPVal2
val ipcomm: String = mainAct.intent.getStringExtra("IPValT2").toString()
You can access like this
(activity as MainActivity).IPVal2
How do I send values that the user puts in editText in StartActivity to textView3 in Second Activity? I am essentially asking the user for his name in StartActivity and then printing "Your name is ___" in the SecondActivity.
I did try to use and then in the SecondActivity but it shows null what do I do?
StartActivity::
button.setOnClickListener
{
val name = editText.text.toString()
val intent2 = Intent (this, SecondActivity::class.java)
intent.putExtra("name", name)
startActivity(intent2)
}
SecondActivity::
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val name2 = intent.getStringExtra("name")
textView3.text = name2
}
The error was that it prints "Your name is null".enter code here
This looks like a typo that Kotlin is helping you make. The name of the intent you're starting is intent2, but you're adding the "name" string to intent. This is possible because Kotlin is letting you access the results of getIntent() (i.e. the Intent object used to launch the current activity) as though it were a property.
Change this line to use intent2 and it should all start working:
intent2.putExtra("name", name)
Change intent.putExtra("name", name) to intent2.putExtra("name", name)
Note: Whenever you don't have a variable intent in your scope. Kotlin extensions take intent value from getIntent() which is your previous intent. And that's why you have missed the compilation error.
I'm trying to get Two strings from two different classes but it's return null for the first intent ( mySPECIALITY ) and the second does not change it's correct.
All the textView returns : null sPECIALITYtxt
val mySPECIALITY = intent.getStringExtra("LEVEL+YEAR+SEMSTER")
val sPECIALITY=intent.getStringExtra("SpecialityNAME")
textViewSPECIALITY.text= "$mySPECIALITY $sPECIALITY"
The first intent i set using this code :
val intent = Intent(this,CalculatingPage::class.java)
intent.putExtra("LEVEL+YEAR+SEMSTER",calledSemYear)
startActivity(intent)
The second intent i set using this code :
val intent = Intent(mContext, CalculatingPage::class.java)
intent.putExtra("SpecialityNAME", mData[position].getspecialityFullName())
mContext.startActivity(intent)
You can't use 2 intents to open 1 activity.
Create 1 intent and put 2 extra values in it:
val intent = Intent(this,CalculatingPage::class.java)
intent.putExtra("LEVEL+YEAR+SEMSTER", calledSemYear)
intent.putExtra("SpecialityNAME", mData[position].getspecialityFullName())
startActivity(intent)
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");