BackPress返回空白活动

时间:2018-10-04 09:39:25

标签: android android-studio android-layout android-fragments onbackpressed

我已经在我的应用中实现了条形码扫描仪。我有一个包含多个片段的活动。在一个片段上,我有一个扫描条形码按钮,然后单击该按钮,它将跳转到扫描活动以扫描代码,并返回包含代码的片段。

但是问题是,当我得到一个get然后打开带有代码的先前片段的对话框时,当我单击工具栏的后退键时,该片段显示白色空白。

 @Override
public void handleResult(me.dm7.barcodescanner.zbar.Result result) {


    String resultCode = result.getContents();

    Log.e("TAG", "handleResult: " + resultCode);
    mScannerView.stopCamera();
    modelProductArticleCodeList = new ArrayList<>();
    modelProductArticleCodeList = databaseHelper.getProductByArtCode(resultCode);


    if (modelProductArticleCodeList.size() == 0) {

        Vibrator vibrator = (Vibrator) getApplicationContext().getSystemService(getApplicationContext().VIBRATOR_SERVICE);
        vibrator.vibrate(1000);


        singleUnrecognisedCheck = new ArrayList<>();

        singleUnrecognisedCheck = databaseHelper.getUnrecognisedByCode(resultCode);

        if (singleUnrecognisedCheck.size() == 0) {

            Intent in = new Intent(this, ContainAllFragmentsActivity.class);
            in.putExtra("unrecognised_alert", true);
            in.putExtra("unrecognised_code", resultCode);
            in.putExtra("jump", "1");
            in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(in);
            finishAffinity();
            int pid = android.os.Process.myPid();
            android.os.Process.killProcess(pid);
            //  Toast.makeText(this, "not matched code", Toast.LENGTH_SHORT).show();

        } else {
            Intent in = new Intent(this, ContainAllFragmentsActivity.class);
            in.putExtra("jump", "3");
            in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(in);
            finishAffinity();
            int pid = android.os.Process.myPid();
            android.os.Process.killProcess(pid);
            Toast.makeText(this, R.string.unrecognised_code_has_been_added, Toast.LENGTH_SHORT).show();
        }


    } else {

       }

        Intent in = new Intent(this, ContainAllFragmentsActivity.class);

        in.putExtra("product_art", resultCode);
        in.putExtra("source_type", "art");
        in.putExtra("jump", "2");
        in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(in);
        finishAffinity();
        int pid = android.os.Process.myPid();
        android.os.Process.killProcess(pid);

    }
}

并根据Intent活动中的关键参数和值切换到特定片段。

if (switchTonext.equals("1")) {
            Fragment fragment = new SearchProductFragment();
            FragmentManager fragmentManager = getSupportFragmentManager(); // this is basically context of the class
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            Bundle bundle = new Bundle();

            bundle.putBoolean("unrecognised_alert", unrecognised_alert);
            bundle.putString("unrecognised_code", getIntent().getStringExtra("unrecognised_code"));

            fragment.setArguments(bundle);
            fragmentTransaction.replace(R.id.frame, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }

为解决此问题,我在活动中的 onBackPressed 中进行了一些更改,并进行了硬编码,如果片段是当前片段的实例,则在单击“后退”按钮时将其更改为上一个片段。

  @Override
public void onBackPressed() {

    Fragment f = getSupportFragmentManager().findFragmentById(R.id.frame);
    if (!(f instanceof SearchShopFragment)) {
        if (f instanceof SearchProductFragment) {
            changeFragments(new SearchShopFragment());
        }
        else if(f instanceof ProductDetailFragment){
            changeFragments(new SearchProductFragment());
        }
        else {
            super.onBackPressed();
        }
    } else {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
        alertDialog.setMessage(R.string.exit_msg);
        alertDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                moveTaskToBack(true);
                android.os.Process.killProcess(android.os.Process.myPid());
                finish();
            }
        });
        alertDialog.setNegativeButton(R.string.no
                , new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        alertDialog.show();
    }

这可以消除空白活动,但是现在当我按“后退”按钮时,我所有的数据都消失了。当我按下返回按钮时,我没有片段中的数据。

请帮助!!我该如何解决这两个问题。

2 个答案:

答案 0 :(得分:0)

有两种方法可以解决此问题, 1)将“扫描代码”的逻辑放在您单击“扫描”按钮后执行操作的同一片段中(避免“扫描活动”)。(单击“按钮”时,使其变得不可见,并显示扫描代码的用户界面)。 ..

2)在获取扫描结果或在ScanActivity中按下后盖时,请在销毁前将片段标签设置在putExtra(“ FRAGMENT_TO_BE_LOAD”,“”)中,并调用ContainAllFragmentsActivity,然后加载相同的片段在标签中设置。为此使用getIntent()。getExtra()...

答案 1 :(得分:0)

在onBackPressed中尝试一下:

FragmentManager fm = getSupportFragmentManager();
    Fragment f = fm.findFragmentById(R.id.frame);

 if(fm.getBackStackEntryCount() > 1){
         fm.popBackStack();
    } else{
        finish();
    }
}