I figured out my problem... In my manifest I had a android:noHistory="true"
in my MainActivity. This prevents the scanning activity in the barcode scanner to pass the result to my MainActivity, because there is no activity on the stack, where it could pass results to.
Using ZXing in Activity with Fragment does not work
-
27-09-2022 - |
Frage
In my App I have a MainActivity
, which makes use of different fragments. One of these fragments is kind of a detail fragment, where I want to capture some serials by scanning a QR code. Currently, I am trying to use the IntentIntegrator
. I am able to scan the code successfully, but after that, my App is not returning correctly. It just displays my MainActivity, but there is no Toast.
I also tried to put a onActivityResult()
in the fragment and a super.onActivityResult()
in the activities onActivityResult()
but it is always the same behaviour. It just jumps into the MainActivity and nothing futher happens.
Can somebody exlain to me, where I made the mistakes? I want to scan the barcode and get the results in my fragment class.
Here is my MainActivity
Code:
public class MainActivity extends Activity {
private Button startenButton, ansehenButton, abmeldenButton;
private FrameLayout fragmentFrame;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentFrame = (FrameLayout) findViewById(R.id.frameLayout);
startenButton = (Button) findViewById(R.id.buttonPruefungStart);
startenButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
if (fragmentFrame.getChildCount() < 1) {
transaction.add(R.id.frameLayout, new PruefungStartenFragment());
} else {
transaction.replace(R.id.frameLayout, new PruefungStartenFragment());
}
transaction.commit();
}
});
}
public void scan() {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.initiateScan();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanResult != null) {
Toast.makeText(this, scanResult.getContents(), Toast.LENGTH_LONG).show();
}
}
}
And here is my PruefungStartenFragment
Code:
public class PruefungStartenFragment extends Fragment {
private TextView datum, ort, pruefer, bedienerdisplay, scanner, fingerprintscanner, quittungsdrucker, barcodeleser,
webcam, kundendisplay, terminal;
private Button scanButton, startButton;
public static PruefungStartenFragment newInstance(String code) {
Bundle args = new Bundle();
args.putSerializable("code", code);
PruefungStartenFragment fragment = new PruefungStartenFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.pruefung_starten_fragment, container, false);
scanButton = (Button) v.findViewById(R.id.buttonScan);
scanButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((MainActivity)getActivity()).scan();
}
});
return v;
}
}
Lösung