"Null" value while retrieving data from intent in kotlin - string

I am passing some value through intent from FirstActivity to MainActivity. But, when I try to retrieve that value from second activity (i.e. MainActivity) I am not able to do so.
FirstActivity
var i = Intent()
launcher?.launch(Intent(this, MainActivity::class.java))
i.putExtra("key", cold)
startActivity(i)
MainActivity
val initialCold = intent.getStringExtra("key")

Your problem is in this line launcher?.launch(Intent(this, MainActivity::class.java)) this is another intent, not i and you launched it without the extra key string, so you can't get it in your MainActivity, the right way if you want to launch activity for result
val intent = Intent(this, MainActivity::class.java)
intent.putExtra("key", cold)
launcher?.launch(intent)
Or like this if you want to start the MainActivity without result
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)

Related

Android Studio My name is not displaying in second activity after passing through first Activity

Main Activity 2
MAIN ACTIVITY
Android Studio My name is not displaying in second activity after passing through first Activity
In your Main Activity:
Please call intent.putExtra(), before startActivity(). Example:
val intent = Intent(this, NewActivity::class.java)
intent.putExtra("username","Name")
startActivity(intent)
And fetch like that (in Main Activity 2)
val bundle :Bundle?=intent.extras
if (bundle!=null){
val user_name = bundle.getString("username")
}
I noticed that in the 'MainActivity.java' you are starting new activity before passing the string into the intent,
just switch the both lines 30 and 31.
In your MainActivity, you should putExtra before startActivity. Like this:
Intent intent = new Intent(...);
intent.putExtra...;
startActivity(intent);
In your MainActivity, create a new Intent:
String name="abcdef;
Intent i = new Intent(MainActivity.this, MainActivity2.class);
i.putExtra("key",name);
startActivity(i);
Then in the MainActivity2, retrieve those values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String name = extras.getString("key");
//"key" must match that used in the other activity
}

Program crashes whenever it gets string data from an another activity

I was just making a simple program that sends string that's typed in an EditText into a TextView in another activity, I'm a beginner and was following a kotlin tutorial on the internet, the program would crash whenever it gets the string from the 1st activity, The code of the 1st activity looks like this:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.button).setOnClickListener {
val message: String? = findViewById<EditText>(R.id.sameer).text.toString()
val kash = Intent(this, kos::class.java)
intent.putExtra("sameer", message)
startActivity(kash)
}
}
}
and the code for the 2nd activity goes like this:
class kos: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.kos)
val kanye: Bundle? = intent.extras
val shaheer = kanye!!.getString("sameer")
Toast.makeText(this, shaheer , Toast.LENGTH_SHORT)
findViewById<TextView>(R.id.UserisHambola).text = shaheer
}
}
I've tried to check when does the error start to happen, it appears that it starts to happen after declaring variable "shaheer" in the 2nd activity, if I delete that code with all the other codes after it, the program doesn't crash, I don't know why does this crash happen, Thank you for your time <3
Try to replace kanye!!.getString("sameer") with kanye?.getStringExtra("sameer") ?: "no data". This way the program will not crash if no value is passed under "sameer" key and the default "no data" will be stored to val shaheer. If you see no data in the toast, you passed the variable between activities incorrectly.
You do this in MainActivity:
val kash = Intent(this, kos::class.java)
intent.putExtra("sameer", message)
startActivity(kash)
You put the extra with the key "sameer" into the Intent referenced by the variable intent. You probably want to put the extra into the Intent referenced by the variable kash. Try this instead:
val kash = Intent(this, kos::class.java)
kash.putExtra("sameer", message)
startActivity(kash)

How can I make intent inside onAdDismissedFullScreenContent() kotlin

I want when the player clicks on the imageView the ads should show first if its ready and then go to the next activity
here is my code
FalgQuizImageView.setOnClickListener {
if (mInterstitialAd != null) {
mInterstitialAd?.show(this)
}
mInterstitialAd?.fullScreenContentCallback = object: FullScreenContentCallback() {
override fun onAdDismissedFullScreenContent() {
val intent = Intent(this, QuizQuestionsCar::class.java)
startActivity(intent)
}
override fun onAdFailedToShowFullScreenContent(adError: AdError?) {
val intent = Intent(this, QuizQuestionsCar::class.java)
startActivity(intent)
}
}
}
Here is the error
First parameter on Intent constructor is of type Context, you are passing this which in your case refers to the FullScreenContentCallback object and its not of type Context. To fix this do the following
val intent = Intent(this#YourActivityName, QuizQuestionsCar::class.java)
this#YourActivityName refers to your Activity object, which is a Context

Add marker in MapBox Android Studio

I have two Activity. I want pass LatLng pass to another Activity.
This code in first Activity:
public void testclick (View v){
Intent intent = new Intent(TestActivity.this, MainMenu.class);
intent.putExtra("lat", "58.37");
intent.putExtra("lan", "37.95");
startActivity(intent);
}
What I must write in another Activity to get LaTLng?
Intent intent = new Intent(getApplicationContext(), MainMenu.class);
intent.putExtra("lat", "`58.37`");
intent.putExtra("lan", "37.95")
startActivity(intent)
And MainMenu Activity onCreate() method receive
if(getIntent().getExtras() != null) {
String lat = getIntent().getExtras().getString("lat");
String lan = getIntent().getExtras().getString("lan");
}

How to send multiple information to the next activity

How can I send information from one activity to two activities? I would like to add another intent but I get an error about a null object... My guess is that I also would need to use another spinner for this class but just wondering if there is a short cut?
add_review.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Review.this, Add_Review.class);
String chosenOption = spinner.getSelectedItem().toString();
intent.putExtra("Add a Review for", chosenOption);
startActivity(intent);
finish();
Intent send_information = new Intent(Review.this, PopularMedicalClinic.class);
String chosenOption2 = spinner.getSelectedItem().toString();
intent.putExtra("Name", chosenOption2);
startActivity(send_information);
finish();
}
This is a wrong approach in your code. You are trying to open two activity at a time. After starting first intent you call finish() method. finish() method kill current activity.
Can you explain why you want to open two activity at a time. Then I can suggest you.
Just use another extra putExtra method.
and remove finish() method. it will destroy your activity.
String chosenOption = spinner.getSelectedItem().toString();
String chosenOption2 = spinner.getSelectedItem().toString();
Intent intent = new Intent(Review.this, Add_Review.class);
intent.putExtra("Add_a_Review_for", chosenOption);
intent.putExtra("Name", chosenOption2);
startActivity(intent);

Resources