App goes to the background after running onActivityResult - java

I am using FirebaseUI to log in to my app. On returning from the login activity, the following onActivityResult-override prints Here1-1, which is as expected. However, immediately after, the activity finishes, which is not what I want. I feel really lost, does anyone have any idea what could be happening?
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode,resultCode,data);
switch (requestCode) {
case RC_SIGN_IN:
if (resultCode == RESULT_CANCELED) {
finish();
} else {
System.out.println("Here" + Integer.toString(requestCode) + Integer.toString(resultCode));
}
case RC_START_APP:
if (resultCode == RESULT_LOGOUT) {
signout();
} else {
finish();
}
}
}

Your cases need a break to stop execution so they don't fall through to the next case. Your activity is actually just finishing due to executing the finish() in the second RC_START_APP case.
switch (requestCode) {
case RC_SIGN_IN:
if (resultCode == RESULT_CANCELED) {
finish();
} else {
System.out.println("Here" + Integer.toString(requestCode) + Integer.toString(resultCode));
}
break; // add this break
case RC_START_APP:
if (resultCode == RESULT_LOGOUT) {
signout();
} else {
finish();
}
break; // add this break
}

Related

How to differentiate between intent passed from an onclicklistener of a cardview child to OnActivityResult

I tried many approaches after reading multiple pages on stackoverflow but I can't seem to get it working.
I have a cardview with onclicklistener to each child clicked (using setSingleEvent).
One of the child uses a barcode scanner then passes the scanned barcode to another activity (via onActivityResult).
But now I need another child (cardview) to use the same barcode scanner and pass the value to another activity as well. I don't know how to differentiate both clicks in the onActivityResult method.
However, the code works fine for a single child grid implementing the barcode scanner (finali == 0). But, in case (finali == 3), how do I pass to the onActivityResult a different thing to do?
private void setSingleEvent(GridLayout mainGrid) {
//Loop all child item of main Grid
for (int i = 0;i<mainGrid.getChildCount();i++){
CardView cardView = (CardView) mainGrid.getChildAt(i);
final int finali=i;
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (finali == 0){
Intent intent = BarcodeReaderActivity.getLaunchIntent(getApplicationContext(), true,false);
startActivityForResult(intent, BARCODE_READER_ACTIVITY_REQUEST);
}
else if (finali == 1){
startActivity(new Intent(getApplicationContext(),AllActiveCheckInActivity.class));
}
else if (finali == 2){
Log.d(TAG, "onClick: Reports Clicked");
}
else if (finali == 3){
Intent intent = BarcodeReaderActivity.getLaunchIntent(getApplicationContext(), true,false);
startActivityForResult(intent,BARCODE_READER_ACTIVITY_REQUEST);
}
}
});
}
}
Here is my onActivityResult that works fine when I make use of only one of the child cardview to implement the barcode scanner.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) {
Toast.makeText(this, "error in scanning", Toast.LENGTH_SHORT).show();
return;
}
if (requestCode == BARCODE_READER_ACTIVITY_REQUEST && data != null) {
Barcode barcode = data.getParcelableExtra(BarcodeReaderActivity.KEY_CAPTURED_BARCODE);
String terminal_id = barcode.rawValue;
Intent intent1 = new Intent(this,LocationDetails.class);
intent1.putExtra("terminal_id",terminal_id);
startActivity(intent1);
}
}
Pass a different constant as requestCode when calling startActivityForResult instead of passing the same one (BARCODE_READER_ACTIVITY_REQUEST) to each call:
if (finali == 0) {
Intent intent = BarcodeReaderActivity.getLaunchIntent(getApplicationContext(), true,false);
startActivityForResult(intent, BARCODE_READER_ACTIVITY_REQUEST_FINALI_1);
}
// ... the remaining code of finali == 1 & finali == 2
else if (finali == 3) {
Intent intent = BarcodeReaderActivity.getLaunchIntent(getApplicationContext(), true,false);
startActivityForResult(intent,BARCODE_READER_ACTIVITY_REQUEST_FINALI_3);
}
And then check the requestCode in the onActivityResult method:
if (requestCode == BARCODE_READER_ACTIVITY_REQUEST_FINALI_1 && data != null) {
// Code for finali == 1
} else if (requestCode == BARCODE_READER_ACTIVITY_REQUEST_FINALI_3 && data != null) {
// Code for finali == 3
}

onActivityResult() never gets called

I have a class used as interface inside my MainActivity.java:
public class prova{
prova(){
}
#JavascriptInterface
public void displayGPSRequest() {
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
#Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
Log.i(TAG, "All location settings are satisfied.");
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to upgrade location settings ");
try {
// Show the dialog by calling startResolutionForResult(), and check the result
// in onActivityResult().
status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
Log.i(TAG, "PendingIntent unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog not created.");
break;
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
//final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
switch (requestCode)
{
case REQUEST_CHECK_SETTINGS:
switch (resultCode)
{
case Activity.RESULT_OK:
{
// All required changes were successfully made
Toast.makeText(MainActivity.this, "Location enabled by user!", Toast.LENGTH_LONG).show();
break;
}
case Activity.RESULT_CANCELED:
{
// The user was asked to change settings, but chose not to
Toast.makeText(MainActivity.this, "Location not enabled, user cancelled.", Toast.LENGTH_LONG).show();
break;
}
default:
{
break;
}
}
break;
}
}
});
}
}
Inside the function displayGPSRequest() I have startResolutionForResult() that should call the method onActivityResult() but it never does. I tried to see other posts where they used fragments but I don't have really understood them. I hope you can help with this.
onActivityResult should be overriden in the associated Activity - currently you're just declaring a method in the ResultCallback which never gets called.

Android: Multiple image view

I want two ImageView's on one activity view, One image for the profile picture and the other for the profile cover page.
My code
setupImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bringImagePicker();
}
});
private void bringImagePicker() {
// start picker to get image for cropping and then use the image in cropping activity
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1,1)
.start(SetupActivity.this);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
mainImageURI = result.getUri();
setupImage.setImageURI(mainImageURI);
isChanged = true;
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
Now how can I add for profile cover image?
You can use ID for both of them, and change the image with corresponding ID from the intent that holds data, add another condition after receiving the results.
I don't know if this library can handle this or not.
If not this simplest solution to hold state variable that tells you which image to update
feel free to ask for clarification

Putextra keep sending the first entered data

This is the function which is used to send data to the Main activity.
public void sendData(){
String name = Bookname.getText().toString();
String number = Integer.parseInt(Bookpage.getText().toString());
String description = Booknote.getText().toString();
Intent back = new Intent(getApplicationContext(),MainActivity.class);
back.putExtra("Bookname",name);
back.putExtra("Booknote",description);
back.putExtra("number",number);
setResult(RESULT_OK, back);
finish();
}
This is where I used the function.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.Color_picker){
Toast.makeText(this, "ColoRS", Toast.LENGTH_SHORT).show();
}
if(item.getItemId() == R.id.Check_icon){
sendData();
}
return true;
}
}
This is how I get the strings in the Main Activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
Ref c1 = new Ref(data.getStringExtra("Bookname"), data.getStringExtra("Booknote"),data.getIntExtra("number",0),data.getIntExtra("color", Color.RED));
adapter.add(c1);
}
}
My problem is that the data are only written once and when the function should be sending different data it sends the same data as the first entered data.
If you send your activityOnResult with 1 as REQUEST_CODE is OK
The only problem I guess you are having (I do not have much more information) is since I see and adapter, I guess you should call adapter.notifyDataSetChanged() to see changes on your "list"?
Also you can "clear" the intent, to avoid duplicated stuff, every time you go to onActivityResult() you can do this getIntent().removeExtra("key"); to ensure, next time will come different data.
If you do this, you have to do the other logic good to work with this...

How can I iterate through an Arraylist in android studio

I am making an android studio app and I want it to make a TextView disappear when I say a certain phrase. The code saves everything I say into an array list. I want to see if it includes the phrase.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 100:
if (resultCode == RESULT_OK && data != null){
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
boolean yes = Arrays.asList(result.get(0)).contains(InputActivity.Item1);
if(yes == true){
result1.setVisibility(View.GONE);
}
}
break;
}
}
Use
for(String s:result){
if(s.equals(InputActivity.Item1)){
//hide text view
}
}

Categories