Multiple onActivityResult{} in same class? - android

I'm doing a few dialogs pickers on my app and using a switch-case on my onClickListener but dialogs are calling for a onActivityResult{} but app is crashing if I add 2... Can I put them both in the same onActivityResult{} calling each by results codes? Code below:
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.bContacts:
Intent i = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(i, PICK_CONTACT);
break;
case R.id.bRingtone:
String uri = null;
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select ringtone for notifications:");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,RingtoneManager.TYPE_NOTIFICATION);
startActivityForResult( intent, Set_Ringtone);
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data)
{
super.onActivityResult(requestCode, resultCode, data);
Cursor c = getContact(ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
if (c.moveToNext()) {
String name = c
.getString(c
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
callName.setText(name);
String phoneNumber = c
.getString(c
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
callNum.setText(phoneNumber);
Log.d("Cont", "name "+name+" no. " + phoneNumber);
}
}
#Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent)
{
if (resultCode == Activity.RESULT_OK && requestCode == 5)
{
Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (uri != null)
{
String ringTonePath = uri.toString();
Toast.makeText(this, "GOT IT" + ringTonePath, Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "DIDNT", Toast.LENGTH_SHORT).show();
}
}
}

You can handle both intents in the same onActivityResult() method. That's why you send a request code with your intent when you call startActivityForResult(), you get it back when the result comes in to differentiate. Your method could look like this:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case PICK_CONTACT:
// handle the contact result
break;
case Set_Ringtone:
// handle the ringtone result
break;
}
}
Your app most likely crashes because you try to work with details of the result that are not provided if you select the wrong intent (e.g. you try to read a contact name here, a ringtone result will not work with that).

Related

why it shows null on selecting a file from storage?

I want to choose a pdf file on a button click and send that file to a url on another buttonclick. My issue is with selecting the pdf file. I am getting null in "onActivityResult()" method.
My code is
SelectButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Pdf"), PDF_REQ_CODE);
}
});
ActivityResult method is
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PDF_REQ_CODE && resultCode == RESULT_OK && data != null && data.getData() != null) {
uri = data.getData();;
SelectButton.setText("PDF is Selected");
}
}
But I am getting uri as null and getting null pointer exception when trying to get file path from uri.
Try this and tell me!
Button button = (Button) x.findViewById(R.id.buttonStripText);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent,PICKFILE_RESULT_CODE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch(requestCode){
case PICKFILE_RESULT_CODE:
if(resultCode==RESULT_OK){
fileName = data.getData().getPath();
}
break;
}
}

Xamarin.Forms alternative to StartActivityForResult

I am currently trying to get the Uri selected from a RingtoneManager.ActionRingtonePicker. I put in all my extra settings and set the appropriate flag.
However I am only able to call StartActivity(intent). Is there anyway for me to get the selection from the Ringtone Picker and use the intent.getParceableExtra(RingtoneManager.ExtraRingtonePickedUri);. My code is below and if I can replace the context.StartActivity(intent) with a work around then that would be great.
public async Task<string> pickAndReceiveRingtone(string currentUri)
{
Intent intent = new Intent(RingtoneManager.ActionRingtonePicker);
intent.PutExtra(RingtoneManager.ExtraRingtoneShowSilent, false);
intent.PutExtra(RingtoneManager.ExtraRingtoneTitle, "Select a ringtone");
intent.PutExtra(RingtoneManager.ExtraRingtoneShowDefault, false);
intent.PutExtra(RingtoneManager.ExtraRingtoneType, (int)RingtoneType.Alarm);
intent.PutExtra(RingtoneManager.ExtraRingtoneExistingUri, RingtoneManager.GetDefaultUri(RingtoneType.Alarm));
intent.SetFlags(ActivityFlags.NewTask);
//Replace below
/*await*/ context.StartActivity(intent);
//Grab selected uri here
currentUri = ...
return currentUri;
}
Actually, Xamarin.Forms exists in an Activity, OnActivityResult method in Activity still can be called.
In your DependencyService class, use the StartActivityForResult() method like this :
public void StartActivityInAndroid()
{
Intent intent = new Intent(RingtoneManager.ActionRingtonePicker);
intent.PutExtra(RingtoneManager.ExtraRingtoneShowSilent, false);
intent.PutExtra(RingtoneManager.ExtraRingtoneTitle, "Select a ringtone");
intent.PutExtra(RingtoneManager.ExtraRingtoneShowDefault, false);
intent.PutExtra(RingtoneManager.ExtraRingtoneType, (int)RingtoneType.Alarm);
intent.PutExtra(RingtoneManager.ExtraRingtoneExistingUri, RingtoneManager.GetDefaultUri(RingtoneType.Alarm));
//intent.SetFlags(ActivityFlags.NewTask);// remember to delete this
var activity = Forms.Context as Activity;
activity.StartActivityForResult(intent, 0);
}
Then, you can receive the result in OnActivityResult method like this :
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode != Result.Ok)
{
return;
}
else
{
Android.Net.Uri uri = (Android.Net.Uri)data.GetParcelableExtra(RingtoneManager.ExtraRingtonePickedUri);
Log.Debug("onActivityResult====", "" + uri);
Toast.MakeText(this, uri + "", ToastLength.Short).Show();
if (uri != null)
{
switch (requestCode)
{
case 0:
RingtoneManager.SetActualDefaultRingtoneUri(this, RingtoneType.Ringtone, uri);
break;
}
}
}
}

Zxing CaptureActivity.handleDecode()

I am new to use Zxing ,When I click a button I want to scan a Two-dimensional code image .This is my MainActivity.java
private Button scan;
scan = (Button) findViewById(R.id.btn_scan);
scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,CaptureActivity.class);
startActivityForResult(intent, SCAN_CODE);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK) {
return;
}
switch (requestCode) {
case SCAN_CODE:
Intent myIntent=getIntent();
Bundle bundle=myIntent.getExtras();
QR=bundle.getString("QR");
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
it will call CaptureActivity.handleDecode(),and This is CaptureActivity.java
public void handleDecode(Result rawResult, Bitmap barcode, float scaleFactor) {
//I want to get the text in the image.
String result = rawResult.getText();
Intent intent = new Intent();
intent.putExtra("QR", result);
if(result!=null && !"".equals(result))
setResult(RESULT_OK, intent);
else{
setResult(RESULT_CANCELED, intent);
}
finish();
}
But it has exception
enter image description here
and I don't konw why?
You should use data.getStringExtra("QR") in your case, rather than
Intent myIntent = getIntent();
Bundle bundle = myIntent.getExtras();
QR = bundle.getString("QR");
where data is onActivityResult()'s parameter.
And use !result.isEmpty() instead of !"".equals(result).
It's more readable.
Hope it helps you!

Trying to getIntExtra() on an intent in onActivityResult(), always null when using a Chooser?

I am wondering if using a Chooser means you can't send extra custom data to the receiving intent?
I call a file selection intent like this
public static void receiveFiles(final AppCompatActivity activity, int which) {
Intent receiveIntent = new Intent(Intent.ACTION_GET_CONTENT);
receiveIntent.setType("text/comma-separated-values");
String[] mimetypes = {"text/csv", "text/comma-separated-values", "application/csv"};
receiveIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
receiveIntent.addCategory(Intent.CATEGORY_OPENABLE);
receiveIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false);
receiveIntent.putExtra(IMPORT_OPTION_WHICH, which); //this line
activity.startActivityForResult(Intent.createChooser(receiveIntent, "Pick CSV"), REQUEST_IMPORT_CSV);
}
But then in onActivityResult after the user selects a file:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_IMPORT_CSV:
if (data != null) {
Uri uri = data.getData(); //is correct
int which = data.getIntExtra(IMPORT_OPTION_WHICH, -1);
//but this always -1!
}
break;
}
}
}
How can I get it to correctly extract the value I passed into the intent?

onActivityResult() not called in fragment and activity finishes

I have a fragment within an activity and I am using a button to select a notification sound using the ringtone picker:
selectSound.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
if (notification != null) {
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) notification);
}
Log.v("TASKS", "Starting Ringtone picker for reminders");
startActivityForResult(intent, 5001);
}
});
Here's my onActivityResult in the fragment:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.v("TASKS", "onActivityResult in ReminderSettings: " + requestCode);
if (resultCode == Activity.RESULT_OK && requestCode == 5001)
{
Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
SharedPreferences settings =
mActivity.getSharedPreferences("SETTINGS", Context.MODE_PRIVATE);
Editor editor = settings.edit();
if (uri != null)
{
editor.putString("NOTIFICATION_SOUND", uri.toString());
}
else
{
editor.putString("NOTIFICATION_SOUND", null);
}
editor.commit();
}
}
My activity has an onActivityResult but it does make call for unhandled results:
#Override
protected void onActivityResult(
int requestCode, int resultCode, Intent data) {
...
super.onActivityResult(requestCode, resultCode, data);
}
The strange thing is after launching and selecting a ringtone neither onActivityResult in the fragment or the activity are called when I debug and the activity seems to finish - there is no force close or exception on the Logcat. I have tested this now on 2 different devices and I get the same result - I'm using ActionBarSherlock for fragments if thats relevant.

Categories

Resources