Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange issue with intent startActivity causing my scanner to not work properly

So, after I finish my scanner activity with

      btn_take_photo.setOnClickListener(new FloatingActionButton.OnClickListener() {
        @Override
        public void onClick(View view) {
            String carde = cardnumberbox.getText().toString().trim();
            if (carde.matches("")) {
                Toast.makeText(getApplicationContext(), getString(R.string.Skan_Udfyld_Kort_Nummer), Toast.LENGTH_SHORT).show();
                cardnumberbox.requestFocus();
                return;
            }
            Intent i = new Intent(ScanActivity.this, CameraActivity.class);
            i.putExtra("EXTRA_SESSION_ID", carde);
            startActivity(i);

        }
    });

to go to my cam activity so I can take some pictures and go back with

    public void btn_aprove2(View view) {
    Intent i = new Intent(CameraActivity.this, ScanActivity.class);
    String counts = count.getText().toString().trim();
    i.putExtra("EXTRA_SESSION_IDs", counts);
    String carde = cardnumberbox2.getText().toString().trim();
    i.putExtra("EXTRA_SESSION_ID", carde);
    startActivity(i);
    finish();

to the scanneractivity again. My scanner does not work properly but if I then press the back button it does go back to the scanneractivity again instead of my menu so it seems like the scanneractivity is running twice and only 1 of them are functional but is here where it confuses me

cause if do not press the btn_aprove2 button and just use the back button instead i gets the exact same issue but here my scanneractivity is not runned twice as when i press the back button it just takes me back to the menu

a video of the issue

by removing my screen orientation from the manifest (so i can rotate it) my scanner do now work but only if i first rotate to landscape and rotate it back to potrait

and i see in the log is that it is only calling the oncreate when rotating and only on resume and pause on the button's(startactivity/finnish)

I am totally lost on how to get this to work.

on github with api demo and documentation in the wiki and with thoose classes that are being used

like image 292
Kewin Björk Nielsen Avatar asked Mar 24 '17 12:03

Kewin Björk Nielsen


1 Answers

If you just jump to the Camera activity to get some data, I'd recommend you to start the activity for a result (startActivityForResult) without finishing the Scanner activity at all. This would give you a proper working back stack (using back button to go back from Camera to Scanner).

Besides that why are you using i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);? You are starting a new activity and finishing the old one. I don't see why this flag is really needed. What is your android:launchMode in your manifest and are you sure you know what FLAG_ACTIVITY_NEW_TASK is doing and it is what you want?

Anyways from what you told us it looks like your example really should utilize startActivityForResult() without calling finish():

  1. Press menu button on some activity
  2. Start camera and do something
  3. Press some button to start Scanner
  4. Scan something and finish the scanner with the result (or cancel the scanner by clicking back)
  5. Retrieve the result of the scanner in the camera and do something with it or continue with the previous workflow when scanner was cancelled
  6. Once you are finished with your workflow, finish the camera so you end up in your activity where you started the camera
like image 146
xxtesaxx Avatar answered Nov 02 '22 07:11

xxtesaxx