Getting a Null value while passing data through intent to another activity - android

When I do a debug, I can see the first activity sending thecorrect number, but the second activity always gets a null value. Thank you for helping.
First Activity:
val intent = Intent(this#InfBateria,ControlActivity::class.java)
intent.putExtra(EXTRA_PERCENTAGE,batteryPercentage.toString())
Second Activity:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.control_layout)
m_address = intent.getStringExtra(Connect.EXTRA_ADDRESS)
val m_percentage: String? = intent.getStringExtra(InfBateria.EXTRA_PERCENTAGE)
textView9.text = m_percentage
[Edit] Code of the Activity (differente from the First one) that acttualy opens da Second Activity:
val intent = Intent(this,ControlActivity::class.java)
intent.putExtra(EXTRA_ADDRESS,address)
startActivity(intent)

If you don't do anything with this result intent, it will not do anything:
val intent = Intent(this#InfBateria,ControlActivity::class.java)
intent.putExtra(EXTRA_PERCENTAGE,batteryPercentage.toString())
You need to update the intent that you are sending to the second activity, like so:
val intent = Intent(this, ControlActivity::class.java)
intent.putExtra(EXTRA_ADDRESS, address)
intent.putExtra(EXTRA_PERCENTAGE, batteryPercentage.toString())
startActivity(intent)
Then your receiving activity will get both the extras. :-)
If you have Three activities and want to get data from 1st to 3rd you do it like this:
First Activity:
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra(ThirdActivity.EXTRA_PERCENTAGE, batteryPercentage.toString())
startActivity(intent)
Second Activity:
val intent = Intent(this, ThirdActivity::class.java)
intent.putExtra(ThirdActivity.EXTRA_ADDRESS, address)
// Get the data you sent to this activity and re-add it to send again
intent.putExtra(ThirdActivity.EXTRA_PERCENTAGE, getIntent().getStringExtra(ThirdActivity.EXTRA_PERCENTAGE)
startActivity(intent)
Third Activity:
m_address = intent.getStringExtra(EXTRA_ADDRESS)
m_battery = intent.getStringExtra(EXTRA_PERCENTAGE)

Related

How to verify sendBroadcast been send with specific intent?

In test I want to verify that sendBroadcast been send. Here is the code from view layer requireContext().sendBroadcast(myYntent, myPermission)
And here I am launching the fragment in test
#Test
fun broadcastIntent_test() = runBlocking {
val intent : Intent = Intent()
reactions.emit(WidgetFragmentReaction.BroadcastIntent(intent))
launchFragmentInHiltContainer<WidgetFragment> {
// val context = InstrumentationRegistry.getInstrumentation().targetContext
// val shadow = shadowOf(context)
// val shadowedIntent: Intent = shadow.peekNextStartedActivity()
// maybe some of these might help
}
}

Input value not updating in another activity which I pass in Intents

Mine Main Activity :-
var sec = second_activity()
var button1: Button? = null
var button2: Button? = null
var button3: Button? = null
var button4: Button? = null
button1 = findViewById<View>(R.id.button1) as Button
button2 = findViewById<View>(R.id.button2) as Button
button3 = findViewById<View>(R.id.button3) as Button
button4 = findViewById<View>(R.id.button4) as Button
button1?.setOnClickListener {
sec.input = "a"
val intent: Intent = Intent(this, second_activity::class.java)
startActivity(intent)
}
button2?.setOnClickListener {
sec.input = "b"
val intent: Intent = Intent(this, second_activity::class.java)
startActivity(intent)
}
Second activity :-
class second_activity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
private lateinit var toolbar: Toolbar
private lateinit var mDrawerLayout: DrawerLayout
var input : String = " "
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
//getting recyclerview from xml
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
//adding a layoutmanager
recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
//crating an arraylist to store users using the data class user
val users = ArrayList<User>()
//adding some dummy data to the list
//creating our adapter
val adapter = CustomAdapter(this , users)
//now adding the adapter to recyclerview
recyclerView.adapter = adapter
Toast.makeText(this, input , Toast.LENGTH_SHORT).show()
when(input) {
"a" -> {
users.add(User(R.drawable.bc))
}
"b" -> {
users.add(User(R.drawable.bc))
users.add(User(R.drawable.bc))
users.add(User(R.drawable.bc))
users.add(User(R.drawable.bc))
}
}
}
Input value is not updating which I pass from MainActivity. It is always taking the blank value which are present in second activity. I also tried by changing the position of Input. but not worked. please help
Is there any other way to find which button is clicked in MainActivity from Second Activity
Yes, there is another way. Use intent.putExtra("requestCode", requestCode). Then in the second activity you can get that requestCode with getIntent().getExtra().getInt("requestCode").
Yes, you can pass the values from one activity to another activity using the following code,
val intent: Intent = Intent(this, SecondActivity::class.java)
intent.putExtra(name, value)
startActivity(intent)
I have given sample code below, please try this..
In first activity, I send the "language" value.
val intent: Intent = Intent(this, SecondActivity::class.java)
intent.putExtra("language", "tamil")
startActivity(intent)
In second activity, I retrived the "language" value.
Put this code in your second activity onCreate()
val intent: Intent = getIntent()
var language = ""
if (intent != null) {
language = intent.getStringExtra("language")
}
println("Language : $language")

Unable to send string from one activity to another (third activity) in Android Studio using Kotlin

So I have three activities for my app. I am taking the username from the user(on Main Screen) and I want to show it to the last screen (Result Screen). I have tried using string constants and then sending the string value using Intent from one activity to the second and to the third one. But I am not able to see the name on the Result Screen. What is that I am doing wrong ?
I am following all the rules and the app is working fine without errors but the username is not been shown to the screen.
In Constants.kt
const val USER_NAME: String = "user_name"
In MainActivity.kt
val nameEditText = findViewById<TextView>(R.id.name_edit_text)
val name = nameEditText.text.toString()
val Intent = Intent(this, QuizQuestionActivity::class.java)
intent.putExtra(Constants.USER_NAME, name)
startActivity(intent)
In QuizQuestionActivity.kt
private var mUsername: String? = null
mUsername = intent.getStringExtra(CONSTANTS.USER_NAME)
val intent = Intent(this, ResultActivity::class.java)
intent.putExtra(Constants.USER_NAME, mUsername)
startActivity(intent)
In ResultActivity.kt
val username = intent.getStringExtra(Constants.USER_NAME)
tv_name.text = username
This is the whole code I am trying to execute. Anybody please help!!
I have two Observations in your code snippet
1)Do you have two Constant files?
Constant.kt
CONSTANT.kt
As in your QuizQuestionActivity, there is a line
mUsername = intent.getStringExtra(CONSTANTS.USER_NAME)
No matter the file that you refer to, the USER_NAME field string values should be the same.
2)Another thing in MainActivity
val Intent = Intent(this, QuizQuestionActivity::class.java)
intent.putExtra(Constants.USER_NAME, name)
startActivity(intent)
Here variable that you use is "Intent"
val Intent = Intent(this, QuizQuestionActivity::class.java)
and you pass "intent" for starting activity
intent.putExtra(Constants.USER_NAME, name)
startActivity(intent)
Try this
In MainActivity:
val nameEditText = findViewById<TextInputEditText>(R.id.name_edit_text)
val name = nameEditText.text.toString()
val intent = Intent(this, QuizQuestionActivity::class.java)
intent.putExtra(Constants.USER_NAME, name)
startActivity(intent)
In QuizQuestionActivity:-
var mUsername: String? = null
mUsername = intent.getStringExtra(Constants.USER_NAME)
val intent = Intent(this, ResultActivity::class.java)
intent.putExtra(Constants.USER_NAME, mUsername)
startActivity(intent)
In ResultActivity:-
val username = intent.getStringExtra(Constants.USER_NAME)
tv_name.text = username

Intent Activity Utils , not declarated in Manifest - Kotlin, Android

I am trying to achieve Intent Activity Utils as extended functions in Kotlin. They will start activities from other Activity/Fragment. Please suggest to me the best way to do that().
I tried to solve that problem like that:
In Activity I invoke extend functions like that :
when (view.id) {
PROFILE.actionViewId -> {
// Start Activity normally
startNewActivity(MyProfileActivity::class.java)
}
PREPAID_RECHARGE.actionViewId -> {
// Dont start activity and crash
startNewActivityForResult(RechargePrepaidActivity::class.java, RECHARGE_PREPAID_REQUEST_CODE)
// Start Activity normally
this.startActivityForResult(Intent(this, RechargePrepaidActivity::class.java), RECHARGE_PREPAID_REQUEST_CODE)
}
TROUBLE_TICKETS.actionViewId -> {
startNewActivity(TroubleTicketsActivity::class.java) // Start Activity normally
}
HELP.actionViewId -> {
startNewActivity(HelpSectionActivity::class.java) // Start Activity normally
}
IntentUtils.kt:
fun <T : BaseActivity> BaseActivity.startNewActivity(newActivity: Class<T>) {
val intent = Intent(this, newActivity)
this.startActivity(intent)
}
//fun BaseActivity.startNewActivity(newActivity: Class<*>) {
// val intent = Intent(this, newActivity)
// this.startActivity(intent)
//}
fun BaseActivity.startNewActivityForResult(newActivity: Class<*>, requestCode : Int) {
val intent = Intent(this, newActivity::class.java)
startActivityForResult(intent, requestCode)
}
fun BaseFragment.startNewActivity(newActivity: Class<*>) {
val intent = Intent(this.requireActivity(), newActivity::class.java)
this.startActivity(intent)
}
fun BaseFragment.startNewActivityForResult(newActivity: Class<*>, requestCode : Int) {
val intent = Intent(this.requireActivity(), newActivity::class.java)
startActivityForResult(intent, requestCode)
}
and also, the Activity is declared in the Manifest - Kotlin, Android
<activity
android:name=".ui.recharge.RechargePrepaidActivity"
android:configChanges="locale|orientation|screenSize"
android:screenOrientation="portrait" />
This starts activity normally:
startNewActivity(MyProfileActivity::class.java) // use extend func
startActivity(Intent(this, MyProfileActivity::class.java)) // use inherited Activity func
But when I try to create the same function for starting activity for result via request code it doesn't work, when I use inherited Activity function startActivityForResult() it's ok
// Dont start activity and crash
startNewActivityForResult(RechargePrepaidActivity::class.java, RECHARGE_PREPAID_REQUEST_CODE)
// Start Activity normally
startActivistyForResult(Intent(this, RechargePrepaidActivity::class.java), RECHARGE_PREPAID_REQUEST_CODE)
The strange here is that when I use my IntentUtils.startNewActivityForResult() app crashes with
ActivityNotFoundException, but on normal use of Fragment.startActivityForResult() it doesn't have this behaviour:
Caused by: android.content.ActivityNotFoundException:
Unable to find explicit activity class {/java.lang.Class}; have you declared this activity in your AndroidManifest.xml?
at ui.utils.view.IntentUtilsKt.startNewActivityForResult(IntentUtils.kt:19)
at ui.main.hamburgerMenu.HamburgerMenuActivity.onClick(HamburgerMenuActivity.kt:117)
The problem was in the duplicated ::class.java invocation :
WRONG:
fun BaseActivity.startNewActivityForResult(newActivity: Class<*>, requestCode : Int) {
val intent = Intent(this, newActivity::class.java) //duplicated ::class.java
startActivityForResult(intent, requestCode)
}
RIGHT:
fun BaseActivity.startNewActivityForResult(newActivity: Class<*>, requestCode : Int) {
val intent = Intent(this, newActivity) // removed ::class.java
startActivityForResult(intent, requestCode)
}

How To Move Activity Form Fragment "Kotlin"

How do you move the Activity from the Fragment? using Kotlin
btnGlobal.setOnClickListener {
val intent = Intent (getActivity(), Main2Activity::class.java)
getActivity()?.startActivity(intent)
}`
Probably you are willing to go to another activity named Main2Activity from your current activity. If it is then the process is as follows:
btnGlobal.setOnClickListener {
val intent = Intent (requireActivity(), Main2Activity::class.java)
startActivity(intent)
// For finishing current activity use below line
// requireActivity().finish()
}
If you are in a fragment do the following:
btnGlobal.setOnClickListener {
val intent = Intent (activity!!, Main2Activity::class.java)
startActivity(intent)
}
If you are on an Activity do the following:
btnGlobal.setOnClickListener {
val intent = Intent (TheNameOfThisActivity#this, Main2Activity::class.java)
startActivity(intent)
}

Categories

Resources