简体   繁体   English

按下后退按钮时如何调用QR码扫描仪?

[英]How to invoke QR code scanner when back button is pressed?

Let's say my app have these requirements: 假设我的应用程序具有以下要求:

  1. First display login screen (if no login session is detected on SQLite) 首先显示登录屏幕(如果在SQLite上未检测到登录会话)
  2. If login is success, display QR code scanning screen (full screen) 如果登录成功,则显示QR码扫描屏幕(全屏)
  3. If scanning is success, store the result on SQLite and display them on the Product screen 如果扫描成功,则将结果存储在SQLite上并将其显示在“产品”屏幕上
  4. If you are in Product Screen and presses back, immediately the QR code scanning screen appears. 如果您在产品屏幕中并按回去,则立即显示QR码扫描屏幕。 And so on. 等等。

Here's my code (irrelevant parts are ommited). 这是我的代码(不相关的部分被省略)。

LoginActivity.java LoginActivity.java

public class LoginActivity extends AppCompatActivity {

    private EditText edtUsername;
    private EditText edtPassword;
    private Button btnLogin;

    // store username & password in SQLite db
    // taken from https://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/
    SessionManager session;

    // manage stored items in SQLite
    private DatabaseHelper db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        spinner.setAdapter(adapter);

        db = new DatabaseHelper(this);

        session = new SessionManager(getApplicationContext());
        if (session.isLoggedIn()){
            IntentIntegrator scanIntegrator = new IntentIntegrator(LoginActivity.this);
            scanIntegrator.setOrientationLocked(false);
            scanIntegrator.initiateScan();
        }

        edtUsername = (EditText) findViewById(R.id.edtName);
        edtPassword = (EditText) findViewById(R.id.edtPassword);
        btnLogin = (Button) findViewById(R.id.btnLogin);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (scanningResult != null){
            String scanContent = scanningResult.getContents();
            String[] data = parseContent(scanContent);

            db.insertItem(new Item(data[0], data[1], data[2]));

            Intent iii = new Intent(LoginActivity.this, ScanResultActivity.class);
            startActivity(iii);
        }
    }

}

ScanResultActivity.java ScanResultActivity.java

public class ScanResultActivity extends AppCompatActivity {

    private RecyclerView recView;
    private ItemAdapter mAdapter;
    private DatabaseHelper db;
    private List<Item> itemList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_result);

        recView = (RecyclerView) findViewById(R.id.recycler_view);
        itemList = new ArrayList<>();

        db = new DatabaseHelper(this);
        itemList.addAll(db.getAllItems());

        mAdapter = new ItemAdapter(this, itemList);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recView.setLayoutManager(mLayoutManager);
        recView.setItemAnimator(new DefaultItemAnimator());
        recView.addItemDecoration(new MyDividerItemDecoration(this, LinearLayoutManager.VERTICAL, 16));
        recView.setAdapter(mAdapter);
    }
}

Part 1 - 3 run fine. 第1-3部分运行良好。 The only issue is once you are in 4 and press the back button, you'll jump to Login activity on its "not login state", so you have to repeat the login process. 唯一的问题是一旦您进入4,然后按返回按钮,您将跳至其“非登录状态”的登录活动,因此您必须重复登录过程。 What I want, instead, is run the QR code scanning part again. 相反,我想要的是再次运行QR码扫描部分。 How to do this? 这个怎么做?

You could isolate your login screen. 您可以隔离登录屏幕。 Instead of only two activities, you could have three: LoginActivity -> QRCodeScanActivity -> ScanResultActivity . 除了两个活动,您还可以拥有三个: LoginActivity > QRCodeScanActivity > ScanResultActivity

This way if the user presses the back button he is going back to QRCodeScanActivity . 这样,如果用户按下返回按钮,他将返回QRCodeScanActivity

in onBackPressed(); onBackPressed(); method just call finesh() method you will be go back on QRCodeScanActivity 方法只需调用finesh()方法,您将返回QRCodeScanActivity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM