startActivityForResult returns to the wrong activity - java

I am working on an app which uses a custom camera (with Surfaceview and such), I am using startActivityForResult from my ObjectActivity to go to the activity with the camera named CameraActivity. This happens in this method.
public void addPicture(View v) {
final CharSequence[] items = { "Take Photo", "Choose from Gallery", "Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(ObjectActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
executeOnResume = false;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
int hasWriteExternalStoragePermission = checkSelfPermission(Manifest.permission.CAMERA);
if (hasWriteExternalStoragePermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
REQUEST_CODE_ASK_PERMISSIONS);
}
}
Intent intent = new Intent(ObjectActivity.this,CameraActivity.class);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[item].equals("Choose from Gallery")) {
executeOnResume = false;
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
REQUEST_SELECT_FILE);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
To be more specific, it happens at these lines:
Intent intent = new Intent(ObjectActivity.this,CameraActivity.class);
startActivityForResult(intent, REQUEST_CAMERA);
This works pretty well, but when I try to return to ObjectActivity after taking a picture, which happens here:
Camera.PictureCallback mPicture = new Camera.PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
//TODO Code to process picture taken
//create a new intent...
Intent intent = new Intent();
intent.putExtra("data",data);
//close this Activity...
setResult(Activity.RESULT_OK, intent);
finish();
}
};
It returns to the activity previous to the ObjectActivity named MainActivity, while it's supposed to go back to ObjectActivity and call onActivityResult() :
#Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
executeOnResume = false;
loadStuff();
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CAMERA || requestCode == REQUEST_SELECT_FILE) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Description");
alertDialog.setMessage("Enter Description");
final EditText input = new EditText(this);
alertDialog.setView(input);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
alertDialog.setView(input);
alertDialog.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
description = input.getText().toString();
if (description == null || description.equals("")) {
description = "-";
}
try {
savePhoto(requestCode,data);
} catch (IOException e) {
e.printStackTrace();
}
}
});
final AlertDialog.Builder tmpDialog = alertDialog;
final AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setTitle("Direction");
dlgAlert.setMessage("Stand with your phone facing the same direction as the picture made and press Ok");
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mBearingProvider.updateBearing();
bearing = mBearingProvider.getBearing();
cardinalDirection = bearingToString(bearing);
Log.e("Direction", cardinalDirection + "," + bearing);
tmpDialog.create().show();
dialog.dismiss();
}
});
dlgAlert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
tmpDialog.create().show();
}
});
dlgAlert.create().show();
}
}
}
But it never gets there.
Does anyone know why this happens?

Get rid of that getParent. You want to set the result for the current activity, not your parent. So replace:
//close this Activity...
if (getParent() == null) {
setResult(Activity.RESULT_OK, intent);
} else {
getParent().setResult(Activity.RESULT_OK, intent);
}
with
setResult(Activity.RESULT_OK, intent);

I've found the solution, if I don't send over a byte[] with the Intent (but instead maybe write out a file and send the path of that file with the Intent), the code seems to work and the ObjectActivity opens.
Part that I removed/changed:
intent.putExtra("data",data);
I don't know why this works, I'd like to know why, but for now I'm happy it works.

Related

startActivityForResult in DialogFragment - QR reader

I have app, which turn on camera QR reader onClick in Fragment,
after that it turns on ServiceActivity
and when you click FAB it turns on TeaDialogFragment, where you click button scan QR it turns on camera QR reader
and it should give result of QR to new Activity (TeaFromQrActivity). But here is a problem - method onActivityResult doesn't called anyway.
TeaDialogFragment.java
public class TeaDialogFragment extends DialogFragment {
public static int BARCODE_READER_REQUEST_CODE = 2;
String qrTeaCode;
private String TAG = TeaDialogFragment.class.getSimpleName();
public TeaDialogFragment() {
// Empty constructor required for DialogFragment
}
public static TeaDialogFragment newInstance(String title) {
TeaDialogFragment frag = new TeaDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
frag.setArguments(args);
return frag;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setTitle(title);
alertDialogBuilder.setMessage("Message");
alertDialogBuilder.setPositiveButton("scan QR", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// This code works - it turns on new Activity
// Intent i = new Intent(getActivity().getApplicationContext(), TeaFromQrActivity.class);
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// getActivity().getApplicationContext().startActivity(i);
// This code doesn't works - it doesn't turn on newActivity, when QR Reader have result, and it turn on current Activity
Intent teaDialogIntent = new Intent(getActivity().getApplicationContext(), BarcodeCaptureActivity.class);
teaDialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivityForResult(teaDialogIntent, BARCODE_READER_REQUEST_CODE);
}
});
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (dialog != null) {
dialog.dismiss();
}
}
});
return alertDialogBuilder.create();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == BARCODE_READER_REQUEST_CODE) {
if (resultCode == CommonStatusCodes.SUCCESS) {
if (data != null) {
Barcode barcode = data.getParcelableExtra(BarcodeCaptureActivity.BarcodeObject);
Intent testIntent = new Intent(getActivity().getApplicationContext(), TeaFromQrActivity.class);
qrTeaCode = "teaQrCode";
testIntent.putExtra(qrTeaCode, barcode.displayValue);
// Start the new activity
startActivity(testIntent);
Log.d(TAG, "Barcode read: " + barcode.displayValue);
} else {
Log.d(TAG, "No barcode captured, intent data is null");
}
} else {
Log.e(TAG, String.format(getString(R.string.barcode_error_format),
CommonStatusCodes.getStatusCodeString(resultCode)));
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
TeaFromQrActivity.java
public class TeaFromQrActivity extends AppCompatActivity {
private String teaQR;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tea_from_qr);
teaQR = getIntent().getExtras().getString("teaQrCode", "null");
Toast.makeText(TeaFromQrActivity.this, "Text from qr: " + teaQR, Toast.LENGTH_SHORT).show();
}
}
Please tell me where I'm wrong.
launch barcode activity like this
Intent intent = new Intent(this, BarcodeCaptureActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, BARCODE_READER_REQUEST_CODE);
more detail refer BarcodeCaptureActivity,barcodereader

How to give Choice of Numbers to Call using Intent.ACTION_CALL?

I am trying to give the option of two numbers on a click of a button to make a call on the number directly but when I am selecting one of the numbers it is going to the dialer screen but "Ljava.lang.CharSequence" is written there and a dialog box with a message service is not supported pops up.
Here is my code:-
phone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final CharSequence numbers[] = new CharSequence[] {"02212345678","+14356789809"};
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Select number to call");
builder.setItems(numbers, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
String call = "tel:" +numbers;
callIntent.setData(Uri.parse(call));
startActivity(callIntent);
}
});
builder.show();
}
});
Change
String call = "tel:" +numbers;
To
String call = "tel:" +numbers[which];
phone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String number = "02212345678";
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Select number to call");
builder.setItems(number , new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
String call = "tel:" +number ;
callIntent.setData(Uri.parse(call));
startActivity(callIntent);
}
});
builder.show();
}
});
This is the complete code for giving choice of numbers and calling the selected number on a Button Click. I hope this will help other members too.
phone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final CharSequence numbers[] = new CharSequence[] {"+1xxxxxxxxx","+1xxxxxxxxxx"};
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Select number to call");
builder.setItems(numbers, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
String call = "tel:" +numbers[which];
callIntent.setData(Uri.parse(call));
startActivity(callIntent);
// the user clicked on colors[which]
}
});
builder.show();
}
});

Alertdialog doesn't show when within if statement

I'm building a small app that scans barcodes and put them in a database (using ZXing library). After the scan is taken, and the barcode doesn't exist in the database I want to show a popup dialog that allows an user to enter the name manually.
The dialog works perfectly if it's invoked in an onClickListener, however when I try to put it in an 'if' statement within another method (which checks if the scan result is in the database) the dialog doesn't show.
What's strange, I put tags in the beginning of the method and at the end, both of them are shown in the log, but the alertdialog doesn't appear and it gets me back to the main activity without any error...
public void popUP (String barcode) {
Log.d(TAG, "method started");
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_input, null);
TextView tv = (TextView) mView.findViewById(R.id.textView2);
final EditText et = (EditText) mView.findViewById(R.id.item1);
Button bt1 = (Button) mView.findViewById(R.id.button2);
Button bt2 = (Button) mView.findViewById(R.id.button3);
final String code = barcode1;
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mydb.addNewItem(et.getText().toString(), code);
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
}
});
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, MainActivity.class);
startActivity(i);
}
});
mBuilder.setView(mView);
AlertDialog dialog = mBuilder.create();
dialog.show();
Log.d(TAG,"dialog shown");
}
And here is the method from which I try to inflate the popUP
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
final String barcode = scanResult.getContents();
if (scanResult != null) {
if(scanResult.getContents() == null) {
Log.d(TAG, "Cancelled Scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d(TAG, "SCANNED");
int rows = mydb.checkRows(barcode);
if (rows == -1){
Log.d(TAG, "popUP should appear");
popUP(barcode); //INVOKING THE ALERT DIALOG
} else {
Log.d(TAG, "found and attempting adding");
mydb.addItem(barcode, rows);
}
}
} else {
Toast.makeText(this, "oddal zero", Toast.LENGTH_LONG);
}
Intent intynt = new Intent(this, MainActivity.class);
startActivity(intynt);
}

Is it possible to use a same onClick for both AlertDialog and View?

My activity has an accept button witch sends intent to parent through onClickOkayButton method. I have overriden the onBackPressed() method so that it shows an AlertDialog asking the user if he really wishes to leave or if he wants to save the preferences, witch does the exact same action as the accept button.
Is it possible to merge both onClick methods into one so I don't need to copy/paste in case of editing one of those, even if both methods use different parameters?
public void onClickOkayButton(View view) {
// THIS IS THE ACCEPT BUTTON
EditText editText = (EditText)findViewById(R.id.surveyadd_name_edittext);
String title = editText.getText().toString();
if (!(title.matches("")) || !(title.isEmpty()) || !(title.equals("")) ) {
Intent intent = new Intent();
intent.putExtra("title", title);
setResult(RESULT_OK, intent);
finish();
} else {
Toast.makeText(this, R.string.surveyadd_warn_notitle, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onBackPressed() {
if (adapter.getCount() != 0) {
showAlertDialog();
} else {
super.onBackPressed();
}
}
public void showAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(R.string.surveyadd_warn_back_message);
builder.setNegativeButton(R.string.surveyadd_warn_back_save, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// THIS WAS COPIED FROM THE ACCEPT BUTTON
EditText editText = (EditText)findViewById(R.id.surveyadd_name_edittext);
String title = editText.getText().toString();
if (!(title.matches("")) || !(title.isEmpty()) || !(title.equals("")) ) {
Intent intent = new Intent();
intent.putExtra("title", title);
setResult(RESULT_OK, intent);
finish();
} else {
Toast.makeText(context, R.string.surveyadd_warn_notitle, Toast.LENGTH_SHORT).show();
}
}
});
// UP TO HERE
builder.setPositiveButton(R.string.surveyadd_warn_back_erase, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
You won't be able to use the same callback methods because they are from different interfaces.
What you should do is move all the common code into a third method and just call it from both click handlers like below:
private void setResultAndFinish() {
EditText editText = (EditText)findViewById(R.id.surveyadd_name_edittext);
String title = editText.getText().toString();
if (!TextUtils.isEmpty(title)) ) {
Intent intent = new Intent();
intent.putExtra("title", title);
setResult(RESULT_OK, intent);
finish();
} else {
Toast.makeText(context, R.string.surveyadd_warn_notitle, Toast.LENGTH_SHORT).show();
}
}
And then the handlers:
public void onClickOkayButton(View view) {
setResultAndFinish();
}
#Override
public void onClick(DialogInterface dialog, int which) {
setResultAndFinish();
}

Dialog Box disapears without being clicked

public void onClick(View v) {
if(a){
Intent i = new Intent();
if(type.equals("x")){
showErrorAlert("string");
i = new Intent(Activity1.this, Activity2.class);
i.putExtra("label", var);
i.putExtra("label1", var2);
startActivity(i);
}
else if(type.equals("y")){
i = new Intent(Activity1.this, Activity3.class);
i.putExtra("label2", var3);
}
//startActivity(i);
}
else startActivity(new Intent(Activity1.this, Activity4.class));
}
});
private void showErrorAlert(String errorMsg){
AlertDialog errorDialog = new AlertDialog.Builder(this).create();
errorDialog.setTitle("title");
errorDialog.setMessage(errorMsg);
errorDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
errorDialog.show();
}
So what happens is the error alert shows, but it will immediately close and the next activity will appear. I want the activity to not start until after "Okay" is selected.
Try this
public void onClick(View v) {
if(a){
Intent i = new Intent();
if(type.equals("x")){
showErrorAlert("string");
}
else if(type.equals("y")){
i = new Intent(Activity1.this, Activity3.class);
i.putExtra("label2", var3);
}
//startActivity(i);
}
else startActivity(new Intent(Activity1.this, Activity4.class));
}
});
private void showErrorAlert(String errorMsg){
AlertDialog errorDialog = new AlertDialog.Builder(this).create();
errorDialog.setTitle("title");
errorDialog.setMessage(errorMsg);
errorDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent i = new Intent(Activity1.this, Activity2.class);
i.putExtra("label", var);
i.putExtra("label1", var2);
startActivity(i);
}
});
errorDialog.show();
}
Move your startActivity(i); to inside if (or) else (or) both blocks depending on where you need.
Place startActivity(i); inside the onClick method in your dialog like this:
private void showErrorAlert(String errorMsg){
AlertDialog errorDialog = new AlertDialog.Builder(this).create();
errorDialog.setTitle("title");
errorDialog.setMessage(errorMsg);
errorDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
startActivity(i);
}
});
errorDialog.show();
}

Categories