Receiving result in android - java

I am beginner to android..I Integrated qr scanner in my app..problem is I added qr scanner button in two fragment..for receiving result I added on activityResult method..In my mainactivity..Is any another method for receiving result in android Instead of OnactivityResult method..If not their I should I add multiple onActivityResult method In my Mainactivty..can anyOne help me..
below is the my code for onActivityresult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult scanResult =IntentIntegrator.parseActivityResult(requestCode, resultCode,
data);
if (scanResult != null) {
if (scanResult.getContents() == null) {
Log.d("ScanFragment", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, " " + scanResult.getContents(), Toast.LENGTH_SHORT).show();
}
} else {
//result fragment
super.onActivityResult(requestCode, resultCode, data);
}
// }
}

static final int FRAGMENT_ONE_REQUEST = 1; // The request code
static final int FRAGMENT_TWO_REQUEST = 2; // The request code
then use this from fragment one
startActivityForResult(intent, FRAGMENT_ONE_REQUEST);
then use this from fragment two
startActivityForResult(intent, FRAGMENT_TWO_REQUEST);
in each fragment
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK) {
//some code
if (requestCode == FRAGMENT_ONE_REQUEST) {
}
}
}

Related

How do I pass ArrayLists to another activity, modify it, and return it back to use?

I'm trying to create a login/register part of a project, and I'm having trouble with passing the sign-up information back to the login activity. I initialized username_info, password_info, and name_info in MainActivity, and I want to send it to SignUpActivity through Intent.
Intent i = new Intent(this, SignUpActivity.class);
i.putExtra("username_info", username_info);
i.putExtra("password_info", password_info);
i.putExtra("name_info", name_info);
startActivityForResult(i, 101);
After values are added in the other activity, it's sent back like this (the arraylists have the same name in both activities):
Intent r = new Intent();
r.putExtra("username_info", username_info);
r.putExtra("password_info", password_info);
r.putExtra("name_info", name_info);
setResult(Activity.RESULT_OK, r);
finish();
}
and it's received here:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
if (resultCode == Activity.RESULT_OK) {
Bundle e = getIntent().getExtras();
username_info = e.getStringArrayList("username_info");
password_info = e.getStringArrayList("password_info");
name_info = e.getStringArrayList("name_info");
}
}
}
But the array lists are unchanged when I get back to the MainActivity. I'm new to Android Studio, so I might just be making a simple mistake.
EDIT:
I'm crashing when the username and password don't match, but it should be returning a toast instead:
#Override
public void onClick(View v) {
username = username_input.getText().toString();
password = password_input.getText().toString();
int index = username_info.indexOf(username);
if (username_info.size() < 1) {
Toast.makeText(MainActivity.this, "You must sign up first", Toast.LENGTH_SHORT).show();
}
else if (password_info.get(index).equals(password)) {
Toast.makeText(MainActivity.this, "make an activity", Toast.LENGTH_SHORT).show();
// open activity
}
else {
Toast.makeText(MainActivity.this, "Incorrect username/password", Toast.LENGTH_SHORT).show();
}
}
});
Don't use getIntent() in onActivityResult, instead use the Intent data:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
if (resultCode == Activity.RESULT_OK) {
Bundle e = data.getExtras();
username_info = e.getStringArrayList("username_info");
password_info = e.getStringArrayList("password_info");
name_info = e.getStringArrayList("name_info");
}
}
}

How to connect 2 onActivityResult?

I have problem with adding a Facebook Authentication because I'm using Google Authentication too and this is why I have error
public class SignUpActivity extends AppCompatActivity {
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
//Google
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
// ...
}
}
}
//and Facebook
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
How you can see I have two onActivityResult methods. Is there any way to connect them and get rid of an error ?
This is how looks like my error
method onActivityResult(int,int,Intent) is already defined in class
SignUpActivity
It's just a communicate of existing two the same methods.
Thanks.
onActivityResult is a Android method, it receives results from activities you started with startActivityForResult and returns the request_code int you provided.
The solution is to use different REQUEST_CODES at startActivityForResult, so you can compare then in onActivityResult
like:
private static final int FACEBOOK_REQUEST_CODE = 1;
private static final int GOOGLE_REQUEST_CODE = 0;
startActivityForResult(googleLoginIntent, GOOGLE_REQUEST_CODE)
startActivityForResult(facebookLoginIntent, FACEBOOK_REQUEST_CODE)
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GOOGLE_REQUEST_CODE) {
//do the code for google result
} else if (requestCode == FACEBOOK_REQUEST_CODE) {
// do the code for facebook result
}
}

Android Java, Setting text of a field outside of the application

I'm currently writing an Android application that will allow a user to input the result of a scanned barcode into a field outside of the application.
The app will simply be a Floating Button that when pressed, will bring up a camera screen that scans and reads the barcode being pointed at, and save the result as an intent.
The user will primarily be working on Chrome when using this Floating Button.
Question: Is it possible to set the text of the current field in focus, outside of the application, as the result of the scanned barcode?
I'm using the following libraries:
- com.google.zxing:core:3.3.3
- com.journeyapps:zxing-android-embedded:3.5.0#aar
Current code:
public class MainActivity extends AppCompatActivity {
FloatingActionButton fabScan;
TextView textViewScanResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Activity activity = this;
textViewScanResult = findViewById(R.id.textViewScan);
fabScan = findViewById(R.id.floatingActionButton_scan);
fabScan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan Barcode");
integrator.setCameraId(0);
integrator.setBeepEnabled(true);
integrator.setBarcodeImageEnabled(false);
integrator.setOrientationLocked(false);
integrator.initiateScan();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Toast.makeText(getApplicationContext(), "nothing", Toast.LENGTH_SHORT).show();
} else {
textViewScanResult.setText(result.getContents());
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
Currently, i'm only setting a textView as the scanned result.
try setting browser intent with the result content.
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Toast.makeText(getApplicationContext(), "nothing", Toast.LENGTH_SHORT).show();
} else {
textViewScanResult.setText();
//here
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.stackoverflow.com")); //change url to google
intent.putExtra(result.getContents().toString());
startActivity(intent);
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}

Zxing Compare Result against local Data?

#Override
public void onActivityResult ( int requestCode, int resultCode, Intent data){
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Log.d("MainActivity", "Cancelled");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
Current working method without comparing against Data^
Below is method where Result is compared with Data to see if there is a duplicate(not working)
#Override
public void onActivityResult ( int requestCode, int resultCode, Intent data){
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == 555821282,8882288,888822884,1145698,248635887,4477996) {
Log.d("MainActivity", "Yes");
Toast.makeText(this, "Yes", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "No");
Toast.makeText(this, "No: " + result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
I get the Integer is too large. I have 500 UPC to put there but I cant even put one. Can I put the numbers in a file somewhere on Android Studio and reference it there? What is a solution to this problem?
IntentResut.getContents() returns a String. To compare two strings in Java, you should use the String.equals(String) method. Your onActivityResult should look like this
#Override
public void onActivityResult ( int requestCode, int resultCode, Intent data){
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
String data = result.getContents();
if (data != null) {
if (data.equals("555821282")
|| data.equals("8882288")
|| data.equals("888822884")
/* ... rest of the options ... */ ) {
Log.d("MainActivity", "Yes");
Toast.makeText(this, "Yes", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "No");
Toast.makeText(this, "No: " + result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
// No data
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
Since you have 500 UPCs to compare, you probably don't want to write a 500 line if-statement. Instead I'd recommend putting all the UPCs (as strings) into a List and then your if-statement can check if the list contains the barcode.
List<String> knownUpcs = Arrays.asList("555821282", "8882288", "888822884", "1145698", "248635887", "4477996");
...
if (knownUpcs.contains(data)) {
// do your thing
}
Here the list is defined statically, but there is no reason you couldn't create the list by reading the UPCs from a file on disk.

Google Voice Recognition Android onActivityResult

In my google voice recognition, I'm trying to add commands. The commands should be in this piece of code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
// add commands
}
}
I am guessing it would be a else if statement but I'm confused on how to start it.
if() {
// put code here
}
But what should I put in the if and the put code here?
You should check the requestCode and resultCode like this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SystemData.VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
// TODO something with matches
}
super.onActivityResult(requestCode, resultCode, data);
}
The 'matches' ArrayList will contain all the recognized words in order.

Categories