What is the meaning of requestCode in startActivityForResult - android

I'm wondering if I am understanding the concepts of requestCode correectly. What is this integer for and does it matter what integer I set it to in:
private static int CAMERA_REQUEST = ???;
Thank you

The requestCode helps you to identify from which Intent you came back. For example, imagine your Activity A (Main Activity) could call Activity B (Camera Request), Activity C (Audio Recording), Activity D (Select a Contact).
Whenever the subsequently called activities B, C or D finish and need to pass data back to A, now you need to identify in your onActivityResult from which Activity you are returning from and put your handling logic accordingly.
public static final int CAMERA_REQUEST = 1;
public static final int CONTACT_VIEW = 2;
#Override
public void onCreate(Bundle savedState)
{
super.onCreate(savedState);
// For CameraRequest you would most likely do
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
// For ContactReqeuest you would most likely do
Intent contactIntent = new Intent(ACTION_VIEW, Uri.parse("content://contacts/people/1"));
startActivityForResult(contactIntent, CONTACT_VIEW);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == Activity.RESULT_CANCELED) {
// code to handle cancelled state
}
else if (requestCode == CAMERA_REQUEST) {
// code to handle data from CAMERA_REQUEST
}
else if (requestCode == CONTACT_VIEW) {
// code to handle data from CONTACT_VIEW
}
}
I hope this clarifies the use of the parameter.

Look my example here. The integer you have to set can be any one positive. Only do not make them the same, you don't want to mix them, do you? And don't put them to 0 - it is returning without result, IMHO, I had strange behaviours with 0. As for negatives, don't use them, too, they are reserved for negative results in other callActivities functions.

public void onActivityResult(int requestCode, int resultCode, Intent data)
app receives results from the different intents via above method only. So how will you understand which intent had replied to you? For that reason, before invoking the intents we put a self defined TAG/Label which is called requestCode. By our own defined requestCODE we can check which intent's result we have received.
Here in requestCode in the example I have given 1001 for Camera Intent. You can put any of your desired number. 1200 or 2001 or 21. Any Positive integers ranging to ~2^16.
See the picture attached.

Using an integer out of the range will raise a "java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode" exception.
So keep in mind that request is not just any positive integer but an integer of 16 bits, so from 0 to 65535.
Similarly, validateRequestPermissionsRequestCode in FragmentActivity requires requestCode to be of 8 bits, so between 0 and 255.

Related

Xamarin Android: using StartActivityForResult(); resultCode in OnActivityResult is always "Canceled"

I have two separate applications written using Xamarin.Android; for the sake of discussion, let's call them "Tristan" and "Isolde". Tristan has some state information that Isolde sometimes needs to know. Complication: Tristan may or may not be running at the moment Isolde develops the need to know his state.
I've got kludge working now where Isolde sends a special launch intent to Tristan, who then uses a broadcast intent to send information back to Isolde. (See my earlier question for details.)
"But wait!" I hear you cry, "Is this not a perfect use case for StartActivityForResult()?" Indeed it is! The code is a whole lot simpler, and everything I've read implies that this is how Android wants you to do stuff like this.
Unfortunately, I can't get it to work (despite trying many variations and reading the dozen-or-so related questions on this very site). My specific problem is that in Isolde's OnActivityResult() callback, the resultCode is always Result.Canceled and the data is always null.
Here is the code for Tristan (where commented-out bits represent variations I've tried):
using Android.App;
using Android.Content;
namespace com.example.Tristan.Android
{
[Activity(Name ="com.example.Tristan.Android.IsoldeQueryActivity")]
public class IsoldeQueryActivity : Activity
{
protected override void OnStart()
{
// base.OnStart();
var rtn = new Intent();
rtn.PutExtra("Test", "test");
//rtn.SetAction("TestAction");
SetResult(Result.Ok, rtn);
Finish();
//FinishActivity(1234);
}
}
}
And here is the relevant code from the Activity where Isolde needs to ask for Tristan's state:
private TaskCompletionSource<bool> TristanStateCompletion;
public async Task GetTristanState()
{
TristanStateCompletion = new TaskCompletionSource<bool>();
var req = new Intent("com.example.Tristan.Android.IsoldeQueryActivity");
//req.PutExtra(Intent.ExtraReturnResult, true);
StartActivityForResult(req, 1234);
var rtn = await TristanStateCompletion.Task;
if (!rtn) bomb("can't get state");
TristanStateCompletion = null;
}
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if(requestCode == 1234) {
DoStuffWith(data);
TristanStateCompletion?.TrySetResult(true);
}
}
Diagnostics -- or rather, a specific lack of them -- leads me to believe that Tristan's IsoldeQueryActivity.OnStart() is never actually being called.
Ideas, requests for additional information and/or useful experiments to try are all welcome. (If your idea is "Put <thing> in the manifest", remember this is Xamarin.Android and I have to do that by putting <relatedThing> in the attribute decorating the Activity.)
Edited to add: In Isolde's code, DoStuffWith(data) was crashing because data was null. When I changed that method to avoid that, I found that I got a (slightly later) exception thrown in StartActivityForResult():
Android.Content.ActivityNotFoundException No Activity found to handle Intent { act=com.example.Tristan.Android.IsoldeQueryActivity }
This leads me to believe I'm not creating the Intent properly in Isolde. Do I need to be using one of the other Intent constructors? If so, how specifically?
Okay, I think I have this figured out. The code in my original question had three major problems:
I was building the Intent incorrectly in Isolde.
I didn't export the IsoldeQueryActivity in Tristan.
The call to base.OnStart() in Tristan's OnStart override is mandatory.
Here is the working version of Tristan:
using Android.App;
using Android.Content;
namespace com.example.Tristan.Android
{
[Activity(Name ="com.example.Tristan.Android.IsoldeQueryActivity", Exported=true)]
public class IsoldeQueryActivity : Activity
{
protected override void OnStart()
{
base.OnStart();
var rtn = new Intent();
rtn.PutExtra("Test", "test");
SetResult(Result.Ok, rtn);
Finish();
}
}
}
And here is the fixed code from Isolde:
private TaskCompletionSource<bool> TristanStateCompletion;
public async Task GetTristanState()
{
TristanStateCompletion = new TaskCompletionSource<bool>();
var req = new Intent();
req.SetComponent(new ComponentName("com.example.Tristan.Android", "com.example.Tristan.Android.IsoldeQueryActivity"));
StartActivityForResult(req, 1234);
var rtn = await TristanStateCompletion.Task;
if (!rtn) bomb("can't get state");
TristanStateCompletion = null;
}
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if(requestCode == 1234) {
if(resultCode != Result.Ok) bomb("bad resultCode {0}", resultCode);
if(data == null) bomb("null data from Tristan");
DoStuffWith(data);
TristanStateCompletion?.TrySetResult(true);
}
}

startActivityForResult for capturing image via the camera returns diffrent requestcode?

I'm developing an android app with the ability to attach images from the camera.
The attach button found in two different activities, and the request code that returns from the camera when finishing is different from one activity to another, here is my code:
File f = new File(Environment.getExternalStorageDirectory()+"/aaa/temp", "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 10);
onActivityResult the returned requestCode is NOT 10 but "65546" and from the other activity the requestCode is "327690" !!!
Why does the camera activity discards the "10" that I have sent when starting it??
Is it bug? Is there any workaround?
I've had the same problem. Create a public static final int in each Activity and use the correct one as requestCode. Also, if you are calling startActivityForResult(...) from a Fragment, try changing it with 'getActivity().startActivityForResult(...)'
You need to manually set your request and results codes from both Activities (requesting and responding) which means you need to have your own Camera-activity implementation class. You should post the code from that (Camera-Activity) class here for us to help you.
A good way to be safe with correct request/result codes as stated in other answers, is to declare public static int codeX for each request/result code and use these variables in every call.
To assist you further, here is an example on how to implement concurrent request and result codes for the two communicating activities:
Requesting Activity
int requestCode=1;
int resultOk=0;
Intent intent = new Intent(getApplicationContext(), Responding.class);
startActivityForResult(intent,requestCode);
Responding Camera-Activity
int resultOk=0;
Intent returnIntent = new Intent();
// you can use this to pass your stuff to the Requesting activity
returnIntent.putExtra("extraStuff",stuff);
setResult(resultOk,returnIntent);
finish();
Requesting Activity
int resultOk=0;
#Override
protected void onActivityResult(int request, int result, Intent data) {
super.onActivityResult(request, result, data);
if (result == resultok && request == requestCode) {
//get your extra stuff from intent
int result = data.getIntExtra("extraStuff", 0);
} else {
// Handle different scenarios
}
}
onActivityResult the returned requestCode is NOT 10 but "65546" and from the other activity the requestCode is "327690" !!!
Why does the camera activity discards the "10" that I have sent when starting it??
Is it bug? Is there any workaround?
You're starting the activity from a fragment and receiving the result in the activity. That's the problem.
You need to start the activity and get the result either both in the fragment, or both in the activity. Do not mix the two.
What happens is that in the v4 support library fragments, fragment index is encoded in the top 16 bits of the request code and your request code is in the bottom 16 bits. For example, 65546 is really 1 << 16 + 10 and 327690 is 5 << 16 + 10. This fragment index is later used to find the correct fragment to deliver the result to.

onactivityforresult not called after contactscontract action_edit

I have a ListActivity which displays all the aggregate contacts. When the user clicks one, my code calls startActivityForResult. All this works properly. When the user finishes editing, I want my ListActivity to be displayed again. Instead, the "people" activity gets displayed. Similarly, my onActivityResult function never gets called.
Here is the code handling the click:
#Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id)
{
Cursor cur = ((SimpleCursorAdapter)parent.getAdapter()).getCursor();
cur.moveToPosition (position);
String key = cur.getString (2); // "2" is the col containing the LOOKUP_KEY
System.out.println ("clicked " + key);
// make intent to edit contact
Intent intent = new Intent (Intent.ACTION_EDIT);
intent.setData (Uri.parse (ContactsContract.Contacts.CONTENT_LOOKUP_URI + "/" + key));
startActivityForResult (intent, 2);
}
And I also have an onActivityResult function:
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
System.out.println ("request " + requestCode + ", result " + resultCode);
}
Any suggestions?
I filed a bug to android about this. Someone looked at it and responded that there is an undocumented workaround. From the bug report:
The undocumented workaround is to call putExtra("finishActivityOnSaveCompleted", true); on the ACTION_EDIT Intent.
However, as this is undocumented, I have no idea which Android version(s) will use it.
I tried it and it works for the version of Android I'm using: 4.1.2. See issue 39262 for more info.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == 2) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}
Replace your onActivity result to this. for request code get after
they will work
Add
super.onActivityResult(requestCode, resultCode, data);
in OnActivtyResult().
Make sure you are calling startActivityForResult from an activity, then only your onActivityResult will be called. For example, if you have the similar code in an fragment, the onActivityResult will never be called.

How can I do different results for scans from Barcode Scanner with three different buttons on Android with new IntentIntegrator

I have three buttons that onlongpress bring up my new IntentIntegrator scans via Barcode Scanner. I successfully have/had it scanning and using the code scanned to do something with when it is only one button.
How can I pass a value or something that when "protected void onActivityResult" is called it will know which button it came from so I can do different things with it depending on which button was long pressed.
My current setup is like this:
button1.setOnLongClickListener(this);
button2.setOnLongClickListener(this);
button3.setOnLongClickListener(this);}
}
public boolean onLongClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.initiateScan();
return true;
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
case IntentIntegrator.REQUEST_CODE:
if (resultCode == Activity.RESULT_OK) {
IntentResult intentResult =
IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (intentResult != null) {
String contents = intentResult.getContents();
String format = intentResult.getFormatName();
//do stuff with the scan. But I want to do different stuff depending on which button was pressed.
Two options:
One, you can modify the IntentIntegrator source code so that it takes different REQUEST_CODES as constructor parameters (or just add a setRequestCode parameter). Then in onActivityResult, check for which requestCode was returned (each of your buttons would return a different request code). This would be the recommended approach.
Second option is the hacky one: In your code, create a member variable that tracks which was the last button pressed (check whether the view passed in onLongClick matches button1,button2, or button3), and use that information in onActivityResult to choose what to do with the results passed back from barcode scanner.

Android: Unable to restart an ListActivity

First post, so please go easy.
I have an app with a handful of tabs, the first is opened on running the app.
One of the tabs is 'My Account' (a ListActivity) showing account options. I switch to this and if the user is not logged in, it, in turn, runs a UserLogon activity using the following:
Intent logonActivity = new Intent(getBaseContext(), UserLogon.class);
startActivityForResult(logonActivity, 0);
To catch the result, I use the following code:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0){
MainBlock ta = (MainBlock) this.getParent();
TabHost th = ta.getMyTabHost();
th.setCurrentTab(0);
finish();
}
if (requestCode == 100)
{
showAccountOptions();
}
}
In the UserLogon class, I have the usual fare; TextView's and Button's. The intention is that if the user cancels the login, it will revert to the initial tab, or if login is successful, it will show the Account Options. And this is indeed what does happen.
All good so far.
The problem I'm having is that if I cancel the login and return to the first tab, when I select the Accounts tab again, I'm not presented with the UserLogon activity. I was under the impression that finish() would close the UserLogon activity, and the Accounts activity but it would appear not.
So, my question is simply, how do I, in effect, restart the Accounts activity so that the user would be presented with the option to login once more.
We're good people and all willing to help ;-) I'll give it a shot. Still, I'm not quite sure I get that all right.
Basically you have an TabActivity which you setup and where you do something like that:
myTabHost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
if (tabId.equals("account") && !loggedIn) {
Intent logonActivity = new Intent(getBaseContext(), UserLogon.class);
startActivityForResult(logonActivity, 0);
}
}});
You're basically saying that the first Activity start of UserLogon works, but the second one doesn't, right? Did you debugged to that point to check whether you reach the code which starts the activity again?
Update based on comment
Your UserLogon should always provide a result information, here's a blueprint:
public class UserLogon extends Activity {
public void onCreate(Bundle bundle) {
// ... do something ...
// if activity is canceled this will be the "last" result
setResult(RESULT_CANCELED);
}
public void checkLoginOrSomethingLikeThat() {
Intent result = new Intent();
// put your results in this intent
setResult(RESULT_OK, intent);
// close activity since we have the information we need
finish();
}
}
The parent activity which is waiting for the result should do it like that:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// it's good practice to use a constant for 0 like LOGIN_REQUEST
// otherwise you will ask yourself later "What the hell did 0 stand for?"
if(requestCode == 0){
if (resultCode == RESULT_OK) {
// get needed data from data intent and react on that
} else {
// no success, react on that
}
}
// ... I don't know what your resultCode 100 is but handle itwith the same pattern
}

Categories

Resources