onActivityResult get position - android

I have an activity that shows a ListView, the ListView is composed by a TextEdit and a Button.
The button is used to pick the email of a contact and put it in the TextEdit.
The listener of the button is created in my custom array adapter and from this method I'm calling the activity to pick the contact like this:
Intent intent =new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Email.CONTENT_URI);
((Activity) v.getContext()).startActivityForResult(intent, MainActivity.act_pick_contact_mail);
I've created the method onActivityResult in my activity and I can get the email picked by the user, but I don't know from wich position the button was pushed.
This is the code I've written:
Do you know how can I do it ?
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//Pick an email
if(requestCode==MainActivity.act_pick_contact_mail){
try{
if(resultCode==Activity.RESULT_OK){
Uri uri=data.getData();
Cursor emailCur=getContentResolver().query(uri,null, null, null,null);
emailCur.moveToFirst();
String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
//Change the first item, but I need to know the real position
contactos.set(0, new Contacto(email));
adapter.notifyDataSetChanged();
emailCur.close();
}}
catch(Exception e){
Toast.makeText(getBaseContext(), "Email wasn't found", Toast.LENGTH_SHORT).show();
e.getCause();
}
}
}
Thanks in advance.
EDIT: Solved creating an attribute within the adaper with a getter.

I see two options here.
One, save the position in a class attribute of your activity. Then reference it when onActivityResult returns.
Two, use the position as your request code. Eg replace MainActivity.act_pick_contact_mail with the position. Then in onActivityResult assume a request code of >= 0 is valid.

Related

Android - Trying to get reference to a locally stored image with intent results in unexpected behavior

What should be happening:
A user clicks on a button which brings up their phone's image gallery. They select an image via startActivityForResult, which I then get the path for and handle it from there.
What is happening:
When I click the button, the gallery opens up perfectly. But when I select an image, onActivityResult is never called and instead of closing the gallery and going back to the initial activity, the app goes back to the activity before it. Like it's calling onBackPressed or something.
My code for opening the gallery:
profilePicView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1);
}
}
Trying to get the result (this never gets called):
public void onActivityResult(int requestCode, int resultCode, Intent data){
// none of this is every called, including the entire onActivityResult method
if(requestCode == 1){
Uri selectedImageUri = data.getData();
selectedProfilePicPath = getPath(selectedImageUri);
}
}
So all I'm trying to do is get the path to the selected image with onActivityResult. But that is never being called and for some reason this action causes the activity to go back to the previous activity. If it matters, this is an Activity and not a Fragment.
Thanks
The issue was that I had "noHistory=true" defined in the Android Manifest for this Activity. Removing that fixed everything.

How to pass value from activity selected item to previous AlertDialog

The scenario are
1.Open DocActivity which has button,the cosign button show Alert Dialog
2.In the AlertDialog,it will link to people search Activity.
3.User will select item to go back to map value to AlertDialog cosign people field.
My Problem is, in DocActivity how to get the intent data from people search?
I think to override onActivityResult is correct ?
Below code is People Search Activity ,i try to turn back to DocActivity
selectPosition = position;
myAdapter.notifyDataSetChanged();
selectEmployee = peoples[position].toString();
Intent passIntent = new Intent(CompanyContactActivity.this, DocActivity.class);
// TODO Add extras or a data URI to this intent as appropriate.
passIntent.putExtra("employeeInfo", selectEmployee);
setResult(Activity.RESULT_OK, passIntent);
finish();
}
Do you call setResult(int resultCode, Intent data) before finish()in people activity?

Navigate back to app after closing share dialog

I have a share button in my application, whose purpose is to share a string of information with user selected app (for example twitter or gmail). The problem is when you click the share button, the share window appears and the app becomes black in the background. After done sharing, the user is returned to menu / home screen and has to open the app again in order to continue using it from where they left.
What I need is to go back to my app after done sharing.
This is the OnClickListener I used:
shareButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = mContext.getString(R.string.shareText) + " " + profileInfo.getName() + " " + mContext.getString(R.string.shareText2);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.setType("text/plain");
startActivity(shareIntent);
}
}
);
What am I doing wrong here? Any help is appreciated.
Use startActivityForResult() instead of startActivity(). This will return to the starting Activity after the Intent action is completed. See the Getting a Result from an Activity post for an example.
And wait for response by overriding onActivityResult() method :
#Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub if(requestCode == 0) {
// You will get callback here when email activity is exited
// perform you task here
}
There is noting wrong with your share intent code which you mentioned above, although there could be some thing wrong with your onPause() method, you probably doing some thing there, try to debug the code after adding logs So you can track the issue OR put your activity code here So exact issue can be identified.

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.

Dealing with Listeners

I know the listeners are supposed to make life easier, especially in a multi-tasking environment like android, but sometimes (to me) it just makes things harder.
Right now I have an ExpandableList activity which presents a list of events, and I want the user to select the group & child of interest and select a notification sound that will play when that event happens. So the list is set up and I have a setOnChildClickListener set up which runs my SetRingtone method:
protected void SetRingtone(int groupPosition, int childPosition) {
Intent intent = new Intent( RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra( RingtoneManager.EXTRA_RINGTONE_TYPE,
RingtoneManager.TYPE_ALL);
intent.putExtra( RingtoneManager.EXTRA_RINGTONE_TITLE,
"Select Tone");
startActivityForResult( intent, PICK_NOTIFICATION_SOUND);
}
So that method has access to the selected group and child positions. The problem is that to get the ringtone selected from the ringtone selector, you have to set up another lister, onActivityResult:
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri =
data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (uri != null) {
String ringTonePath = uri.toString();
ExpandableListView variableView =
(ExpandableListView)findViewById(android.R.id.list);
int p = variableView.getSelectedItemPosition();
Log.d("phca", "The selected item number was: " +
p + "; The sound selected is: " + uri );
}
}
}
And now I don't have access to the selected group and child. As you can see, I tried to get the position from the getSelectedItemPosition, but it always returns -1 here.
I know I am probably making this harder than it really is.
You could just store the group and child in instance variables before the call to startActivityForResult and then use these instance variables in onActivityResult.
Did you try to use variableView.getSelectedItem()?
If this return null so it mean that something wrong with your list

Categories

Resources