How to pass value from activity selected item to previous AlertDialog - android

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?

Related

Intent is not working, when the state of a button is checked

I am trying to build a flight ticket booking app.
Here I have two options either user selects one-way or round-way(both sides). When a user selects one-way I need to go to the next screen where all one-way flight details will be shown. Other-wise if a user selects round-way then I will show the details of the round trip to the user.
But if I select one-way(Button) and check it's Pressed state it shows False, So I am unable to navigate the user to the next screen and the same flaw is happening for the round-way.
MainActivity
private void searchClickHandeler(){
//Set Onclick Listener
b_search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
*when One way is pressed it is suppose to go to the One way Suggestion Activity*
if (b_oneway.isPressed()){
Toast.makeText(FlightActivity.this,"Oneway Button is pressed",Toast.LENGTH_LONG).show();
Intent oneway = new Intent(FlightActivity.this,FlightSuggestionActivity.class);
startActivity(oneway);
} else if (b_returnway.isPressed()){
Toast.makeText(FlightActivity.this,"RoundTrip Button is pressed",Toast.LENGTH_LONG).show();
Intent returnway = new Intent(FlightActivity.this,FlightRoundTripSuggestionActivity.class);
startActivity(returnway);
}
}
});
}
try this
Intent intent = new Intent(v.getContext(), FlightSuggestionActivity.class);
startActivity(intent);

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.

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.

onActivityResult get position

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.

[Android]How to get results from intent launched from preference screen?

I need help in geting results back from intent launched from
preference screen
// Intent preference
DevicePref =
getPreferenceManager().createPreferenceScreen(this);
// Show a Screen with list of Devices Discovered
Intent i = new Intent(this,getDevice.class);
DevicePref.setIntent(i);
DevicePref.setTitle("Select Device");
DevicePref.setSummary(mSelectedDevice);
deviceOptionsCat.addPreference(DevicePref);
I want user to select device... In preference screeen I show "Select
Device" .. when user clicks that, another screen is launched by intent
where all devices are listed. User selects the device.
Now how do I know user selected which device? And I want to update
that in the summary.
Pls. let me know
Thanks
I got the answer, Hope it will help someone like me...
Do not mention intent while creating preference like I did in above code.. Mention intent on OnPreferenceClickListener and then do StartActivityForResult()
// Intent preference
DevicePref = getPreferenceManager().createPreferenceScreen(this);
// Show a Screen with list of Devices Discovered
DevicePref.setOnPreferenceClickListener(onPreferenceClick);
DevicePref.setTitle("Select Device");
DevicePref.setSummary(mSelectedDevice);
deviceOptionsCat.addPreference(DevicePref);
Then create OnPreferenceClickListner and here do StartActivityFromResult()
OnPreferenceClickListener onPreferenceClick = new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
if (preference ==DevicePref )
{
Intent i = new Intent(DevuiceOptions.this,getDevice.class);
DevicePref.setIntent(i);
startActivityForResult(i,CHOOSE_DEVICE);
}
return true;
}
};
Finally to get the result handle onActivityResult and update Summary field.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch (requestCode) {
case Constants.CHOOSE_DEVICE:
{
if (data!=null )
{
Bundle b = data.getExtras();
mSelectedDevice = (String) b.get("Name");
UpdatePreferences();
}
}
}
}
Thanks

Categories

Resources