Im using the ZXing barcode scanner and I have some issues.
I
m following this intersection, and I dont understand where I should call the "onActivityResult" method, and which parameter I need to send.
I try to put the call under the OnClickListener but I always get from my intentResult null.
This is my code:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment implements View.OnClickListener {
Button scan;
TextView t1, t2;
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
scan = (Button)rootView.findViewById(R.id.bScan);
t1 = (TextView) rootView.findViewById(R.id.tv1);
t2 = (TextView) rootView.findViewById(R.id.tv2);
t1.setText("OK");
scan.setOnClickListener(this);
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
if (intentResult != null ){
String scanContact = intentResult.getContents();
String scanFormat = intentResult.getFormatName();
String contact = "Contact is: " + scanContact +"\n";
String foramt = "Format is: " + scanFormat;
--- I never get to this part ---
t1.setText(contact);
t2.setText(foramt);
} else {
Toast toast = Toast.makeText(getActivity(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
t1.setText("Not OK");
}
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.bScan) {
IntentIntegrator intentIntegrator = new IntentIntegrator(getActivity());
intentIntegrator.initiateScan();
// Intent intent = getActivity().getIntent();
//startActivityForResult(intent,getTargetRequestCode());
//onActivityResult(getTargetRequestCode(),CONTEXT_INCLUDE_CODE,intent);
}
}
}
}
Any help ? Thanks
Your MainActivity that hosts your Fragment, gets the result. You should call onActivityResult in MainActivity, and call super, so that your Fragment will get the chance to handle the data.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}