Multiple onActivityResult for 1 Activity - android

So I have a very simple app I am working on. It's purpose is to collect asset data from 1 pc, and 1 or 2 monitors.
My form contains 3 edittext views, and 3 buttons (one for each asset I am collecting data for). The buttons invoke startActivityForResult for the barcode scanner, then I want to pass the result to the associated edittext view based on which button was pressed (example: press "scan" button to the right of "Asset - PC" edittext, scan and return data to it's associated edittext. Then if you press the button "scan" thats next to the "Asset - Mon1" edittext, return data to "Asset - Mon1" edittext.... and so on...)
With the code I have here, all the items work, just not as intended. Pressing any of the "scan" buttons always returns the result to the first edittext view "Asset - PC".
public class TestShit extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void assetPcClick(View view) {
Intent intent1 = new Intent("com.google.zxing.client.android.SCAN");
intent1.setPackage("com.google.zxing.client.android");
intent1.putExtra("SCAN_MODE", "ONE_D_MODE");
startActivityForResult(intent1, 0);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents1 = intent.getStringExtra("SCAN_RESULT");
String format1 = intent.getStringExtra("SCAN_RESULT_FORMAT");
EditText assetPC = (EditText) findViewById(R.id.assetPC);
assetPC.setText(contents1);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
public void assetMon1Click(View view) {
Intent intent2 = new Intent("com.google.zxing.client.android.SCAN");
intent2.setPackage("com.google.zxing.client.android");
intent2.putExtra("SCAN_MODE", "ONE_D_MODE");
startActivityForResult(intent2, 0);
}
public void onActivityResult2(int requestCode, int resultCode, Intent intent2) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents2 = intent2.getStringExtra("SCAN_RESULT");
String format2 = intent2.getStringExtra("SCAN_RESULT_FORMAT");
EditText assetMon1 = (EditText) findViewById(R.id.assetMon1);
assetMon1.setText(contents2);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
public void assetMon2Click(View view) {
Intent intent3 = new Intent("com.google.zxing.client.android.SCAN");
intent3.setPackage("com.google.zxing.client.android");
intent3.putExtra("SCAN_MODE", "ONE_D_MODE");
startActivityForResult(intent3, 0);
}
public void onActivityResult3(int requestCode, int resultCode, Intent intent3) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents3 = intent3.getStringExtra("SCAN_RESULT");
String format3 = intent3.getStringExtra("SCAN_RESULT_FORMAT");
EditText assetMon2 = (EditText) findViewById(R.id.assetMon2);
assetMon2.setText(contents3);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
}
Any suggestions on how I can better manage my multiple "ActivityForResult" and "onActivityResult" 's ?
My fix, thank you for all your help!
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents1 = intent.getStringExtra("SCAN_RESULT");
String format1 = intent.getStringExtra("SCAN_RESULT_FORMAT");
EditText assetPC = (EditText) findViewById(R.id.assetPC);
assetPC.setText(contents1);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
String contents1 = intent.getStringExtra("SCAN_RESULT");
String format1 = intent.getStringExtra("SCAN_RESULT_FORMAT");
EditText assetMon1 = (EditText) findViewById(R.id.assetMon1);
assetMon1.setText(contents1);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
if (requestCode == 2) {
if (resultCode == RESULT_OK) {
String contents1 = intent.getStringExtra("SCAN_RESULT");
String format1 = intent.getStringExtra("SCAN_RESULT_FORMAT");
EditText assetMon2 = (EditText) findViewById(R.id.assetMon2);
assetMon2.setText(contents1);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}

In your startActivityForResult, don't use 0's on both calls... use different numbers like 0 & 1... then you can implement a switch in your onActivityResult method with the requestCode. If the requestCode = 0 then the first method has returned, if it is 1, then the second has returned. This should be the same for more calls.
public void onActivityResult(int requestCode, int resultCode, Intent intent){
switch(requestCode){
case 0: // Do your stuff here...
break;
case 1: // Do your other stuff here...
break;
case etc:
break;
}
}
The calls should be something like this then:
(For the first time)
startActivityForResult(intent1, 0);
(For the second time)
startActivityForResult(intent2, 1);
(for the third time)
startActivityForResult(intent3, 2);
(for the nth time)
startActivityForResult(intentn, n - 1);
Or, you could declare static int values to use, instead of the more unrecognisable int values.

While you startActivityForResult you send a requestcode with it,
this should be different(unique) for your every activity you are starting from your button, say button 1 starts activity request code 1, button 2 requestcode = 2, and button 3 request code =3, then for your parent activity you must have only one onActivityResult()
in this function take a switch case , scan requestcodes, requestcode = 1 will give result from first activity and request code =2 gives for activity 2 and so on...

There's nothing in Android that is ever going to recognize and call methods named onActivityResult2 or onActivityResult3. Those are just method names you made up that are going to be ignored by the system.
You need to change your code such that you pass a different request code when you call startActivityForResult(). (requestCode is the 2nd parameter to that method)
Then in onActivityResult check the requestCode to see which activity you are getting the result from, and handle accordingly.

Related

Disable button in a Listview's row while getting response of a save?

Issue:
I have a Listview in Mainactivity. Each row of listview has two buttons say SET and RUN.
Pressing SET will take you to SET activity and if the user clicks save button in SET Activity, I need to disable the SET button in the corresponding row position of the listview in mainactivity.
So Far Done:
For that I have a refresh function on a onclicklistener to requery the list with updated values. How to call that refresh function without keypress in the Mainactivity or is there any other way?
Activity MAIN :
viewHolder.ButtonSET.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String title = v.getTag().toString();
if (title.equals("SET")) {
if (Integer.parseInt((String) viewHolder.TDNQTY.getText()) > 0) {
if(scanoverornot(pos)<=0) {
Intent s = new Intent(DN.this, SETActivity.class);
s.putExtra("position", pos);
s.putExtra("mode", "SET");
try{
startActivityForResult(s, saverequestcode);
// getContext().startActivity(s);
}
catch(Exception e){
Toast.makeText(getContext(),""+e,Toast.LENGTH_LONG).show();
}
}
}
}
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null)
return;
switch (requestCode) {
case saverequestcode:
if (resultCode == RESULT_OK) {
String SItem= data.getStringExtra("SItem");
int SPos= data.getIntExtra("SPos", 0);
saved = 700;
Toast.makeText(getApplicationContext(), ""+ SItem+ SPos, Toast.LENGTH_LONG).show();
//btnvalidate.performClick();
}
}
}
Activity SET :
Intent sav = new Intent();
sav.putExtra("SItem", String.valueOf(itemno));
sav.putExtra("SPos", String.valueOf(pos));
setResult(RESULT_OK, sav);
finish();
You can use startActivityForResult to nail this purpose:
startActivityForResult(new Intent(this, DestinationActivity.class), MY_RESULT);
And then in your MainActivity:
public int MY_RESULT = 10;
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_RESULT) {
if (resultCode == Activity.RESULT_OK) {
//refresh the list according to your logic
}
}
}
Don't forget to call setResult(Activity.RESULT_OK); when user clicks save button.
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setResult(Activity.RESULT_OK);
}
});
Issue Lines:
Have to add super.onActivityResult(requestCode, resultCode, data);
Removed switch case and used if condition for requestCode check
Solution:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data == null)
return;
if (requestCode == saverequestcode) {
if (resultCode == Activity.RESULT_OK) {
String SItem = data.getStringExtra("SItem");
String SPos = data.getStringExtra("SPos");
Toast.makeText(getApplicationContext(), "Item :" + SItem + "Position :" + SPos, Toast.LENGTH_LONG).show();
}
if (resultCode == Activity.RESULT_CANCELED) {
//Any methods
}
}
else if (requestCode == importrequestcode){
}
}
Activity SET :
Intent sav = new Intent();
sav.putExtra("SItem", String.valueOf(itemno));
sav.putExtra("SPos", String.valueOf(pos));
setResult(Activity.RESULT_OK,sav);
finish();

onActivityResult no shows what it should

I'm trying to return an EditText that has one Activity to a first Activity but when I push the button to finish de second Activity and onActivityResult is call the TextView that should show the text in the EditText save, no shos nothing.
Here's the Intent creation and the starts of the second Activity:
Intent myInt = new Intent(ActivityLoaderActivity.this, ExplicitlyLoadedActivity.class);
startActivityForResult(myInt, GET_TEXT_REQUEST_CODE);
Here's how I save the EditText in ExplicitlyLoadedActivity.java:
private void enterClicked() {
Log.i(TAG,"Entered enterClicked()");
// TODO - Save user provided input from the EditText field
Editable res = mEditText.getText();
// TODO - Create a new intent and save the input from the EditText field as an extra
Intent returnIntent = new Intent();
returnIntent.putExtra("result", res);
// TODO - Set Activity's result with result code RESULT_OK
setResult(RESULT_OK,returnIntent);
// TODO - Finish the Activity
finish();
}
And here's is the code of onActivityResult:
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String res = data.getStringExtra("result");
mUserTextView.setText(res);
}
}
In the debug mode, I can see that res has the text that I put in the EditText but it don't shos nothing in the aplication.
This is the code of mUserTextView:
private TextView mUserTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loader_activity);
// Get reference to the textView
mUserTextView = (TextView) findViewById(R.id.textView1);
This is the full code of onActivityResult():
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Entered onActivityResult()");
// TODO - Process the result only if this method received both a
// RESULT_OK result code and a recognized request code
// If so, update the Textview showing the user-entered text.
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String res = data.getStringExtra("result");
mUserTextView.setText(res);
}
}
}
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String res = getIntent.getStringExtra("result");
mUserTextView.setText(res);
}
}
try that piece of code
and make sure GET_TEXT_REQUEST_CODE==1
I've tried the same code as you describe above, it works well. Since res is really received in onActivityResult you should check your mUserTextView related code.

Use the result of a QR code scan to open an image in a new android activiy

I have a program for an android application which has 2 activies MainActivity and CarteActivity. I have a button on the MainActivity layout (activity_main.xml) which launches a QR code scanner (already programmed using zxing). The second activity layout (activity_carte.xml) has an ImageView which code is :
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/carte0" />
`
In my drawable folder I copied some pictures named carte1, carte2, carte3...
I also generated a few qrcodes which result is also carte1, carte2, carte3... (as a text)
Now I want that when I scan these qrcodes, the CarteActivity appears and the ImageViewer displays the picture corresponding to the code (when i scan the code which returns carte1 as a result, the picture carte1 appears in the ImageViewer), but I don't know how to.
I noticed that in the MainActivity the result of the scan is memorized in the string "contents" :
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if (requestCode == 0)
{
if (resultCode == RESULT_OK)
{
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
Log.i("xZing", "contents: "+contents+" format: "+format);
// Handle successful scan
}
else if (resultCode == RESULT_CANCELED)
{
// Handle cancel
Log.i("xZing", "Cancelled");
}
}
`
But I don't know how to use it properly.
Could you help me with that ? I'm sure it's not that hard but I can't find how to do it.
Change your MainActivity call ass follows
public void onActivityResult(int requestCode, int resultCode, Intent intent){
if (requestCode == 0)
{
if (resultCode == RESULT_OK)
{
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
Log.i("xZing", "contents: "+contents+" format: "+format);
// Handle successful scan
Intent startNewActivityOpen = new Intent(MainActivity.this, CarteActivity.class);
startNewActivityOpen.putExtra("QRContents", contents);
startActivity(startNewActivityOpen);
}
else if (resultCode == RESULT_CANCELED)
{
// Handle cancel
Log.i("xZing", "Cancelled");
}
}
}
Then do changes on CarteActivity as follows
import android.content.Intent;
import android.widget.ImageView;
public class CarteActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_carte);
Intent bgIntent = getIntent();
String contents = bgIntent.getStringExtra("QRContents");
ImageView img = (ImageView)findViewById(R.id.imageView1);
if(contents.equals("carte1")){
img.setImageResource(R.drawable.carte1);
}
if(contents.equals("carte2")){
img.setImageResource(R.drawable.carte2);
}
if(contents.equals("carte3")){
img.setImageResource(R.drawable.carte3);
}
}
#Override
public void onBackPressed() {
Intent startNewActivityOpen = new Intent(CarteActivity.this, MainActivity.class);
startActivity(startNewActivityOpen);
}
}

zxing barcode - how to embed the scanner(intent) with other layouts

The code below works for me. However, how could I embed it with other layouts from another activity?
Basically, The screen will be splitted in two parts:
Fist one will contain a header and shared buttons to use in other screens;
Second one will be the Barcode scanner or any other screen.
public Button.OnClickListener mScan = new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
};
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
thanks
There isn no way to do this. You are invoking a third-party app and can't control how it looks.

How do I start thread only after first activity is finished?

In my application I have scan button which scan qr code. Code is like this:
btnScan.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 1);
ClearForm();
//if (!CheckCHFID())return;
pd = ProgressDialog.show(EnquireActivity.this, "", getResources().getString(R.string.GetingInsuuree));
new Thread(){
public void run(){
getInsureeInfo();
pd.dismiss();
}
}.start();
}
});
Now the problem is before I scan the code it starts finding the information which is getInsureeInfo(); How can I control that it should execute only after user scans the code successfully?
Thanks in advance.
you need to move the part that you want to happen after the scan to the onActivityResult() method.
/*Here is where we come back after the Barcode Scanner is done*/
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
// contents contains whatever the code was
String contents = intent.getStringExtra("SCAN_RESULT");
// Format contains the type of code i.e. UPC, EAN, QRCode etc...
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan.
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel. If the user presses 'back' before a code is scanned.
}
}
}
Also I think you are going to have to use a Handler to send a message from the work thread to the main thread when it is time to hide your progress dialog. I don't think it will let you call dismiss on it from the background thread. That is just a hunch though, not tested.
put it in OnActivityResult method ,ovveride it.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 1:
if (resultCode == RESULT_OK) {
//put your stuff here....
break;
}
}
}

Categories

Resources