android onActivityResult vs getIntent - android

Both onActivityResult and getIntent receives intent from other activities.
when one is used over another?
I think onActivityResult is called after I call startActivityForResult to receive a result.
I guess then getIntent is used to receive intent which was sent by someone other than me. Is it correct?
Below is my understanding how it's used, wonder if the understanding is correct.
I register to listen to certain intents (probably in manifest)
when registered intents get delivered, a responsible activity resumes (or gets created)
getIntent should generally be placed in onResume to check the intent
one should dispose the used intent by removing whatever data inside the intent

I think onActivityResult is called after I call startActivityForResult
to receive a result.
on onActivityResult is called when you finish() the Activity that you started with startActivityForResult. You can provide an Intent to setResult, which you will get back as part of onActivityResult
I guess then getIntent is used to receive intent which was sent by
someone other than me. Is it correct?
getIntent() returns the intent that started the current Activity

Related

Sending email via Intent.createChooser. Works but returns to different Activity

I have and app which requires sending emails with attachments.
This app contains many activities. Within one activity, I send an email using the following intent:
startActivityForResult(Intent.createChooser(emailIntent, "Email"), EMAIL_REQUEST);
This works fine. The email is sent.
(note: onActivityResult is never called. I can live with this, it's not important)
The problem:
After email is sent, the app loads/goes to the startup/initial activity, not the activity from which the email was sent.
How can I return to the activity which sent the email?
Some observations:
I added some Toast(s) to the onRestart, onPause, and onCreate methods of the startup/initial activity. After the email is sent, the startup activity is displayed but none of the events are called.
If you want to send an email, then return to the previous activity in the stack, you can simply call finish(); when you are done sending the email. This will finish the current Activity, and return to the second-top Activity in the stack.
Update:
Add this code after your email has been sent
Intent intent = new Intent(this, YourActivity.class);
startActivity(intent);
I had exactly the same problem. What was happening in my case is that when I called the email Intent from within my activity, the activities OnPause was being called. The problem was that at the end of the OnPause function code I had a finish(), which was causing the activity to be destroyed !
Removing finish() fixed the problem, although I do think I still have some work to do, because I think I was calling finish() to pass bundle information back to the calling activity. I will have to see about that later...

Differents between sendBroadcast (intent) and startActivity(intent) in android?

When i send a broadcast by context.sendBroadcast(intent1) method with parameter: intent1 and start a activity by context.startActivity(intent2) method with parameter: intent2. What is difference between them. Are intent1 and intent2 implicit intent with define: new intent(action_do_something). Can anyone help me ? Thank a lot
As the names suggest, sendBroadcast will send a message to no particular recipient. It just transmits a message like a radio tower. You have to listen for broadcasts.
On the other hand, startActivity starts an activity(the onResume() of the activity will be eventually called).
The names are saying it all. When you send a broadcast message you need to call the sendBroadcast. but to start activity u need the other method. There is no direct comparision between these two as the purpose of these two things is totally different.
In both case the intent is used for a common reason, first two define the receiver. In the broadcast message the intent is passed to make sure which type of receiver can catch this. And for startActivity it is used to do same but for to make sure which activity will be started. and in both cases intent is used to pass data.

How to differentiate From Which Activity the current intent comes

Form Activity Login I call
Intent intent=new Intent(LogIn.this,BRInfoActivity.class);
From Activity BrActivityList I Call
Intent intent=new Intent(BRActiviList.this, BRInfoActivity.class);
intent.putExtra("BRinfo", objBR);
And Now in BRInfoActivityList
Intent i = getIntent();
BrInfoEN objBR=(BrInfoEN)i.getSerializableExtra("BRinfo");
Now i want if(intent==BrActivityList)The last two above line execute otherwise does not.How to do it?
If you will start activity with
startActivityForResult().
Then you can use getCallingActivity() to get caller.
Form Activity Login call this--
Intent intent=new Intent(LogIn.this,BRInfoActivity.class);
intent.putExtra("call_from","ActivityClass");
From Activity BrActivityList Call--
Intent intent=new Intent(BRActiviList.this, BRInfoActivity.class);
intent.putExtra("call_from", "BRActivity");
Now, when you get the intent, compare the "call_from" string in an if-else block! That's it!
May this help you...
The new intent comes as part of onNewIntent(Intent). The original Intent is still available via getIntent().
You put whatever code you need to into onNewIntent in order to update the UI with the new parameters; probably similar to what you're doing in onCreate.
Also, you probably want to call setIntent(intent) in onNewIntent to make sure future calls to getIntent() within the Activity lifecycle get the most recent Intent data.

Intent extras missing when activity started

Inside a broadcast receiver I want to start my app (Activity) and pass in some data.
My problem is that the extras don't seem to carry over into the activity. I am trying to get the data inside the onNewIntent(Intent i) function.
Any ideas?
Here is my current attempt in the BroadcastReceiver:
Intent intSlider = new Intent();
intSlider.setClass(UAirship.shared().getApplicationContext(), SliderMenuActivity.class);
intSlider.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intSlider.putExtra("action", ScreensEnum.Object);
intSlider.putExtra("objectId", objectId);
intSlider.putExtra("objectCode", objectCode);
intSlider.putExtra("userId", userId);
UAirship.shared().getApplicationContext().startActivity(intSlider);
EDIT - Added code used in onNewIntent() and onCreate()
The following code works great in onCreate() when the app isn't currently running. For when the app is already running the same code doesn't work (i.e. no extras) from the onNewIntent() function.
Intent intent = getIntent();
if(intent.hasExtra("objectId")) {
loadDetail(intent.getStringExtra("objectId"), "2w232");
}
The problem is getIntent() method. It always returns the intent that started the activity, not the most recent one. You should use intent that was passed to onNewIntent method as an argument.
We stumbled upon this problem once, when we were trying to launch/call onNewIntent on an Activity in response to a local notification tap. The extras that we put on our Intent were disappearing at the time onNewIntent received it.
I don't remember this being documented anywhere back then, but the "problem" was that we weren't setting the action field on the Intents that we prepared. Turns out if the Intent received by your Activity doesn't have an action set using setAction, the system still delivers the Intent to its destination, but doesn't transmit the extras you have set while creating the Intent.
TL;DR:
If you encounter this problem with an Intent with no action, calling setAction to set an arbitrary action value before sending the Intent might fix it.
Extract from the docs
This is called for activities that set launchMode to "singleTop" in
their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag
when calling startActivity(Intent). In either case, when the activity
is re-launched while at the top of the activity stack instead of a new
instance of the activity being started, onNewIntent() will be called
on the existing instance with the Intent that was used to re-launch
it.
An activity will always be paused before receiving a new intent, so
you can count on onResume() being called after this method.
Note that getIntent() still returns the original Intent. You can use
setIntent(Intent) to update it to this new Intent.
I think the last paragraph explains your problem.
You have to set the flag FLAG_ACTIVITY_SINGLE_TOP or set launchMode singleTop in the manifest file.
Of course when onNewIntent is called you do not use getIntent but the Intent received as argument.
onNewIntent will be called when the activity instance already exists. For example, if last time you pressed the Home Screen button.
I wrote a solution that worked for me here: Intent with old extra in onCreate() for singleTask Activity
You can store the last received intent in a member variable (mLastIntent).
Then you can use this member in your onResume() method to query for your extra data.
private Intent mLastIntent;
#Override
protected void onNewIntent(Intent intent) {
mLastIntent = intent;
};

Android 2.3.3 | Differentiating Intents That Start an Activity

In Android 2.3.3, how does one differentiate Intents that start a particular Activity. For example, if both Activity_A and Activity_B have intents that call startActivityForResult( intent, requestCode), how does Activity_C differentiate between which Activity has started it? Also, I know that one passes a requestCode to the starting Activity, but how does this Activity handle the requestCode? There is no method in Intent that says getRequestCode(). Is the only way to do this to place the requestCode in a Bundle in addition to the method startActivityForResult? Thanks!
Intent API:
http://developer.android.com/reference/android/content/Intent.html
One solution would be to pass along an extra piece of identifying data. For example:
intent.putExtra("activity", "com.whatever.MyActivity");
Then the receiving Activity can read it:
Bundle extras = getIntent().getExtras();
String activityName = extras.getString("activity");
It seems like there should be an easy method call to tell what the sending Intent was, but if so, I'm not aware of it.

Categories

Resources