fun kullaniciOlustur2(view: View){
val intent = Intent(applicationContext,KullaniciOlustur2::class.java)
intent.putExtra("input",makeUsername.text.toString())
intent.putExtra("input2",makeUserphone.text.toString())
startActivity(intent)
}
Mainactivity2 starts here..before this, I was using
val intent = intent
val received: String = intent.getStringExtra("input")
makeUsername.text = received
But this method doesn't work anymore.
I tried using getIntent() but couldn't get anything
val intent = getIntent()
Try this code
Activity 1
val intent = Intent(FirstActivity.this,SecondActivity::class.java) //not application context
intent.putExtra("input",makeUsername.text.toString())
intent.putExtra("input2",makeUserphone.text.toString())
startActivity(intent)
Activity 2
inside onCreate() method use
val stringOne = getIntent().getStringExtra("input")
Or more cleaner way is
val extras = getIntent().getExtras()
if (null != extras) {
val value = extras.getString("input")
//The key argument here must match that used in the other activity
}
and please check similar answers in Java, you may be able to get the Idea here already told in another answer.
I also Use Anko to remove this kind of boilerplate code
I recommend using Kotlin Anko, there are plenty of methods that will help you to remove this boilerplate code
check Anko Intents here
Use Below Code:
The Code is same as Java .Just difference is kotlin do not have
1. No semicolon at the end.
2. to call another activity kotlin use ::
ex.KotlinActivity::class.java
startActivity(Intent(this, KotlinActivity::class.java).putExtra("DataTrasfer", ""))
To Get Value:
intent.getStringExtra("DataTrasfer")
Related
I have 2 activities. And I need pass a lot of variables from one activity to another activity:
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra(ACCOUNT1, accoun1)
intent.putExtra(ACCOUNT2, accoun2)
intent.putExtra(ACCOUNT3, accoun3)
intent.putExtra(ACCOUNT4, accoun4)
intent.putExtra(ACCOUNT5, accoun5)
intent.putExtra(ACCOUNT6, accoun6)
intent.putExtra(ACCOUNT7, accoun7)
intent.putExtra(ACCOUNT8, accoun8)
intent.putExtra(ACCOUNT9, accoun9)
and so on. How can I make this transfer easier? Maybe is some other ways to pass multiply values from one activity to another activity?
I recently had a similar case.
How I solved:
I created an entity that kept all the information for me, that way, I just passed an object between activities, but I had everything I needed inside that object.
Hope this can help.
There is a bundleOf function in the Jetpack core-ktx library that works like mapOf.
val intent = Intent(this, SecondActivity::class.java)
val extras = bundleOf(
ACCOUNT1 to accoun1,
ACCOUNT2 to accoun2,
ACCOUNT3 to accoun3,
// etc.
)
intent.putExtras(extras)
But that doesn't save much code compared to just using apply unless you use the same bundle of extras in multiple places in your code:
val intent = Intent(this, SecondActivity::class.java).apply {
putExtra(ACCOUNT1, accoun1)
putExtra(ACCOUNT2, accoun2)
putExtra(ACCOUNT3, accoun3)
//...
}
You could make a parcelable data class that stores all your values and pass that instead. See instructions here for creating the class easily: https://developer.android.com/kotlin/parcelize
#Parcelize
data class AccountInfo(
val account1: String,
val account2: String,
val account3: String,
//...
): Parcelable
Then you can put this into your bundles, and you don't have to create a bunch of the same kinds of variables in both activities, because all the variables are inside one class.
val intent = Intent(this, SecondActivity::class.java).apply {
putExtra(ACCOUNT_INFO, myAccountInfoInstance)
}
if all of your variables has the same type you can add all to list and pass the list to second activity/fragment.
I'm learning the Activity Results API that replace the onActivityResult. I understand the concept and basic usage, but not sure what's the proper way to setup an intent that takes multiple input extras.
For example, I want to put two Boolean flags and one String to the intent, in the old way, I could do
Val intent = Intent(this, AnotherActivity::java.class)
intent.putExtra(key1, false)
intent.putExtra(key2, true)
intent.putExtra(key3, someString)
But how do I do this in createIntent method? And what would the input type for this custom ActivityResultContract?
One way I can see is using Intent as input type, like class CustomActivityContract : ActivityResultContract<Intent, Unit>(). But that doesn't feel right to me.
Any suggestion is appreciated!
You don't call createIntent() directly. Instead, pass your intent to the ActivityResultLauncher.launch() method.
val launcher = registerForActivityResult(StartActivityForResult()) { result ->
// handle result
}
launcher.launch(
Intent(this, AnotherActivity::class.java)
.putExtra(key1, false)
.putExtra(key2, true)
.putExtra(key3, someString)
)
startActivityForResult(intent,3021)
I am using this since long time, is this method is deprecated now?
It is indeed deprecated. I tried to figure the new way out and thought I want to share it here.
Now instead of overriding onActivityResult for all callbacks and checking for our request code, we can register for each call back separately by using registerForActivityResult which takes in an ActivityResultContracts. IMO this is a way better approach than the previous way.
Here is an example of starting an activity for a result:
val previewRequest =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == RESULT_OK) {
val list = it.data
// do whatever with the data in the callback
}
}
Now instead of StartActivityForResult we use
val intent = Intent(this, PreviewFullscreenActivity::class.java)
intent.putStringArrayListExtra(AppConstants.PARAMS.IMAGE_URIS, list)
previewRequest.launch(intent)
I'm creating an Intent like so:
public void showThings(ArrayList<Thing> things) {
Intent intent = new Intent(this, ThingActivity.class);
intent.putExtra(THINGS, things);
startActivity(intent);
}
Then in the ThingActivity I want to get the ArrayList<Thing>
class ThingActivity {
var things: ArrayList<Thing>? = null
override fun onCreate(savedInstanceState: Bundle?) {
things = intent.extras.getSerializable(OtherActivity.THINGS) as? ArrayList<Thing>
}
Unfortunately, I can't seem to figure out how to cast to the appropriate type without triggering an "unchecked cast" warning. Is there a way to gracefully set to null if (somehow unexpectedly) the cast fails?
Appending ?: return null does not seem to work as I've seen suggested elsewhere
The unchecked cast warning is happening due to the way Java generics work at runtime. Because of type erasure, at runtime, the type of the list is just List, and not List<Thing>. That means that the cast is considered unsafe, even though it's quite possible for a human to look at the code and see that there's no problem.
While I agree with you that suppressing the warning isn't ideal, in this case I think it's fine.
The best solution, though, would be to implement the Parcelable interface on Thing. That way, when you want to pass a List<Thing> through an intent, you could write:
intent.putParcelableArrayListExtra(THINGS, things)
And when you want to read it back out:
things = intent.extras.getParcelableArrayListExtra(OtherActivity.THINGS)
Neither of these will cause a compiler warning.
As an alternative to Ben P's answer, you could use Gson.
Assuming that Things is simply a data class (holds a bunch of variables), this will work perfectly (this is also required by Ben P's answer).
Here's a way to implement it:
public void showThings(ArrayList<Thing> things) {
String json = new Gson().toJson(things);
Intent intent = new Intent(this, ThingActivity.class);
intent.putExtra(THINGS, json);
startActivity(intent);
}
Then you can get the ArrayList like this:
String json = intent.getStringExtra(THINGS);
TypeToken<ArrayList<Things>> token = new TypeToken<ArrayList<Things>>() {};
ArrayList<Things> things = new Gson().fromJson(json, token.getType());
Activity one:
val intent = Intent(this, SecondActivity::class.java)
val arrayGuide = ArrayList<Guide>()
intent.putParcelableArrayListExtra("arrayInfo",arrayGuide)
startActivity(intent)
Activity two:
if(intent != null){
val arrayList =
this.intent.getParcelableArrayListExtra<Guide>("arrayInfo")
}
I have read the artical https://github.com/Kotlin/anko/wiki/Anko-Commons-%E2%80%93-Intents
The Code A is to open a send email activity, I hope to replace it with org.jetbrains.anko.startActivity, how can I do? Thanks!
Code A
fun sendFeedbackByEmail(mContext: Context) {
val data = Intent(Intent.ACTION_SENDTO)
data.data = Uri.parse("mailto:" + mContext.getString(R.string.FeedbackEmailAddress))
data.putExtra(Intent.EXTRA_SUBJECT, mContext.getString(R.string.FeedbackEmailSubject))
data.putExtra(Intent.EXTRA_TEXT, mContext.getString(R.string.FeedbackEmailFirstContent))
mContext.startActivity(data)
}
You should not try to replace with the startActivity function. There is function available in Anko. It goes like this
email(email, [subject], [text])
Arguments in square brackets ([]) are optional. Methods return true if the intent was sent.
You can call it like
email("youremail#yourdomain.com","Your Subject","Your Text")
Right now they don't have feature where you can send same email to multiple email id. You have to make another call.
Anki is achieving is by writing extension over Context and Fragment classes. So this function is available for both Activity and Function