0

I am integrating the zxing scanner into my android application and I am coming across a weird situation where the onActivityResult (in the activity initiating the scan) is being called before the scanning intent opens. I have looked at several examples and my code seems to match what I am seeing in many of the tutorials. Here is the code for the activity.

package com.honeydewit;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.honeydewit.adapters.ListItemAdapter;
import com.honeydewit.listeners.OneOffClickListener;
import com.honeydewit.pojos.BasicList;
import com.honeydewit.pojos.ListItem;

public class ListHomeActivity extends BasicActivity{
   private ImageButton addItemBtn;
    private ImageButton addByScanBtn;
        private ArrayList<ListItem> lists = new ArrayList<ListItem>();
        public static  ListItemAdapter listAdapter;
private TextView headerTxt;
private BasicList basicList;
private ListView listView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listshome);
    listView =(ListView) findViewById(R.id.list);
    basicList = getApplicationContext().getCurrentList();
    headerTxt = (TextView)findViewById(R.id.headerTxt);
    headerTxt.setText(basicList.getName());
    headerTxt.setTypeface(getApplicationContext().getTypeface());
    //add button
    addItemBtn = (ImageButton)findViewById(R.id.add);
    addItemBtn.setOnClickListener(new OneOffClickListener() {

        @Override
        public void onClick(View v) {

            addToList(v, basicList);

        }
    });
    addByScanBtn = (ImageButton)findViewById(R.id.addByScan);
    addByScanBtn.setVisibility(View.VISIBLE);
    addByScanBtn.setOnClickListener(new OneOffClickListener() {

        @Override
        public void onClick(View v) {

            addToListByScan(v, basicList);

        }
    });
    setupListAdapter(basicList.get_id());


}


private void setupListAdapter(int listId) {
    populateListItems(listId);
    listAdapter = new ListItemAdapter(this, R.layout.listrow, lists);
    listView.setAdapter(listAdapter);
}


private void populateListItems(int listId) {
    ArrayList<ListItem> items = (ArrayList<ListItem>)getApplicationContext().getShoppingListDbHelper().getShoppingListItems(listId);
    for(ListItem item : items ) {
        lists.add(item);
    }
}


private void addToList(View view, BasicList list) {

    if(basicList.getListTypeId() == Constants.TODO_LIST_TYPE_CDE) {
        Intent newListIntent = new Intent(getBaseContext(), ToDoItemActivity.class);
        startActivityForResult(newListIntent, 1);
    }
    else {
        Intent newListIntent = new Intent(getBaseContext(), ItemActivity.class);
        startActivityForResult(newListIntent, 1);
    }

}
private void addToListByScan(View view, BasicList list) {
    try {

        IntentIntegrator zxingIntegrator = new IntentIntegrator(this);  
        zxingIntegrator.initiateScan();

    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "ERROR:" + e, 1).show();

    }

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
            Log.d(getClass().getName(), scanResult.getContents());
            Log.d(getClass().getName(), scanResult.getFormatName());
            Log.d(getClass().getName(),data.getStringExtra("SCAN_RESULT_FORMAT"));
            Log.d(getClass().getName(),data.getStringExtra("SCAN_RESULT"));
        }
    }

}



}
pleasantstranga
  • 231
  • 2
  • 14
  • 1
    I found the answer here. http://stackoverflow.com/questions/7910840/android-startactivityforresult-immediately-triggering-onactivityresult – pleasantstranga Feb 15 '14 at 18:08

1 Answers1

1

onActivityResult is called by Android whenever your app needs to be told about an Intent. That could be from lots of places. You know by looking at the request code and comparing it to the one in IntentIntegrator. Or let that class do all this for you.

My guess is your intent-filter is too broad and you're hearing things you don't expect.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173