Use startActivityForResult()
and onActivityResult()
. This is your solution.
How to handle multiple intents in a single activity in android?
-
01-07-2022 - |
Question
I have Three activity classes in my android Application . The first activity has to pass intent to the second and third activity one has to pass intent to the second. In this case ,How can i handle two intents in the same activity . I thought i should use the if ,else condition . But i doubt if there is some technique to handle multiple intents in a single activity?
Solution
OTHER TIPS
Just a simple if else condition :
if (intent.getStringExtra("com.example.ActivityOne.intentOne") != null) {
// do something
} else if (intent.getStringExtra("com.example.ActivityThree.intentThree") != null) {
// do something else
}
If you just want to know which Activity
issued the current Intent
to know what extras
to expect or to know what to do I will sometimes just use an arbitrary String extra
and check what that is. So in sending Activity
something like
Intent i = new Intent(FirstActivity.this, NextActivtiy.class);
i.putExtra("source", "first"); // source is the string to check where it is coming from
startActivity(i);
Then in the receiving Activity
Intent intent = getIntent();
String from = intent.getStringExtra("source");
if ("first".equals(from))
{
// do stuff if from first Activity
}
else
{
// do other stuff
}
Check if extras is not null
if (intent.getExtras() != null) {
String from = intent.getStringExtra("source");
if ("first".equals(from)){
// do stuff if from first Activity
}
else {
// do other stuff
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow