What is the difference between Intent vs intent? - android

this is the code:
override fun itemClicked(id: Long) {
val intent = Intent(this, DetailActivity::class.java)
intent.putExtra(DetailActivity.EXTRA_ID, id.toInt())
startActivity(intent)
}
And now i know this is the explicit intent and i can call with it activity and pass some data. But there is this intent:
intent.putExtra(DetailActivity.EXTRA_ID, id.toInt())
startActivity(intent)
In Android Studio i get tool tip like that "intent" is from getIntent/setIntent . Can someone explain to me little further about this or some links if you understand me what i am trying to ask .
Thank you.

Activity has getIntent/setIntent methods, and such methods (with "get" and "set" in names in java) are represented as properties in kotlin. When you created a variable with the same name, you shadowed this "property". https://kotlinlang.org/docs/reference/java-interop.html#getters-and-setters

Intent is the name of the class, and intent is variable, its name can be i also., intent is just name that you've given to your variable.
val i = Intent(this, DetailActivity::class.java)
i.putExtra(DetailActivity.EXTRA_ID, id.toInt())
startActivity(i)
So in the above there is no intent, but still the code will work, so intent is just name of variable, here you can put any name you want.

Related

getStringExtra() function of intent is returning null

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")

How to send String values from one activity to another through intent?

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.

How to retrieve the activity requested by an Intent

Say I have an Intent like this:
Intent intent = new Intent(context, MyActivity.class);
I then want a method that will return true for the following:
boolean found = intent.getSomeMethodToRetrieveActivity() instanceof MyActivity;
Basically is there any way to find out what Activity the intent resolves to?
any ideas?
EDIT
Perusing the src I can see I can get the class name like this:
intent.getComponent().getClassName()
which will return "com.my.package.MyActivity" which is close but I'd like to use instanceof
I just ended up using equals() like in my question with:
intent.getComponent().getClassName()
What about using Java's reflection mechanism, concretely Class.newInstance() or Class.isInstance() methods?

Android: Who start Activity

I have 3 activity: Activity1, Activity2, Activity3. Activity1 and Activity3 can start Activity2:
startActivity (new Intent(this,Activity2));
How in Activity2 I can check which class start Activity2 ? Is there any way to take information from Intent ? Thanks...
I dont whant to put anything in Intent. I think I dont need that. When I start intent I already saying from what context I do that (new Intent(*this*,Activity2)). And this "this" I want to recognize in Activity2 !!! Is that possible ?
You can pass that information in the "extra" Bundle of your Intent:
In Activity1:
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("caller", "activity1");
startActivity(intent);
In Activity2:
String caller = getIntent().getStringExtra("caller");
There's also get/put extra methods for other data types like boolean, int, etc.
As for why you cannot access the Context you supplied when creating the Intent on the other side:
The only thing that Android does with the supplied Context (i.e. your this) is to create a ComponentName from it. That class only keeps the package of the supplied Context and discards anything else.
So you theoretically could put your activities into different packages and then go getIntent().getComponentName().getPackageName() on it at the receiving end -- but please don't. Intent extras is the way to go.
To send:
Intent intent = new Intent(this,Activity2)
intent.putExtra("activityNumber", 1);
startActivity(intent);
To receive:
Bundle extras = getIntent().getExtras();
if (extras != null) {
activitySource = extras.getInt("activityNumber");
}
You can pass along information between Activities using an Intents "Extras":
Intent newIntent = new Intent(this, Activity2.class);
newIntent.putExtra("CalledFrom", Activity1.class.getSimpleName())
startActivity(newIntent);
Then in the receiving Activity you can call:
String fromActivity = getIntent().getStringExtra("CalledFrom");
You can pass basic types as well as Object's which implement the Parcelable interface
You can do this by adding information to your Intent.
for example:
public Intent putExtra (name, value)
In the newly started service you can then look up this information.
Or you can use the addFlags method like in the other answer.
in the intent that starts activity you can always pus flag as
Intent i=new Intent();
i.putExtra("startedBy","A1");
//or
//i.putExtra("startedBy","A2");
//...
and than in the receiving activity use
in the on activityresult method
Intent intent=getIntent();
if(intent.getStringExtra("startedBy").equals("a1"){
//do somth
}
Use Intent data members to pass parameters to your activities. There's an extras data member that is a Bundle of anything you want - a general-purpose name/value collection.
Intent i = new Intent(this, Activity2);
i.putExtra("From", "Activity1");
startActivity(i);
Then in Activity2 you can examine the starting intent for extra data and find this out.
Alternatively, you can use intent data, settable with setData(). Data member has the format of the URI, somewhat unfriendly to passing structured information. It's slightly less typing though.
I found the solutions that use the simple name of the class cause ClassNotFoundExceptions. The solution I use is similar, but uses the canonical name:
Intent newIntent = new Intent(this, Activity2.class);
newIntent.putExtra("caller", Activity1.class.getCanonicalName())
startActivity(newIntent);
Then in the other activity
String caller = getIntent().getStringExtra("caller");
Class callingClass = Class.forName(caller);

Identify android intent type, from another method/class?

I create an Intent in one class then it's returned to another class. I need to check the type of intent it is. I need to ensure it's not a specific class of intent.
How to do this?
if( newIntent == ActivityName.class )
Edit:
So I would need to know if the new intent would equal an intent of a certain type:
newIntent = new Intent(context, ActivityName.class);
return newIntent;
In another class:
if( newIntent == ActivityName.class ) // do something
Hope this clarifies a bit.
If you're just trying to check what type the intent is (or any java object for that matter) use
if (newIntent instanceof ActivityName){
//do stuff
}
However, I suspect this isn't what you're actually looking to do. Regardless of what activity you create an Intent in, it is always an instance of the Intent class. That is, of course, unless you have explicitly created a subclass of the Intent class. Can you tell us a little more about what you're trying to do?

Categories

Resources