无法在Android中的onActiviyResult()中获取正确的结果代码

时间:2016-10-01 15:58:28

标签: android android-intent android-activity

我有两个不同的活动,我希望与之互动,这意味着我想使用onActivityResult()方法......

但是当我使用它时,我只有这个结果代码:RESULT_CANCELED ...我不明白为什么......

你能帮我吗?

以下是我的MainActivity部分,该部分调用以下SecureKeyActivity

((TextView)layout.findViewById(R.id.profile_forgot_password)).setOnClickListener(new TextView.OnClickListener() {
                                    @Override
                                    public void onClick(View v2) {
                                        dialog = new AlertDialog.Builder(MainActivity.this)
                                            .setTitle("Mot de passe oublié")
                                            .setMessage("L\'envoi de SMS peut engendrer des frais supplémentaires dont la Zwedge Corporation n\'est pas responsable.\nÊtes-vous sûr d\'avoir oublié votre mot de passe ?")
                                            .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface box, int which) {
                                                    Intent secureKey = new Intent(MainActivity.this, SecureKeyActivity.class);
                                                    secureKey.putExtra("securePass", Common.data.requestByName(((TextView)v.findViewById(R.id.connect_profile_pseudo)).getText().toString()).getPassword());
                                                    secureKey.putExtra("securePseudo", ((TextView)v.findViewById(R.id.connect_profile_pseudo)).getText().toString());
                                                    startActivityForResult(secureKey, 2890, null);
                                                }
                                            })
                                            .setNegativeButton(R.string.no, null)
                                            .create();
                                        dialog.show();
                                    }
                                });

MainActivity.onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO: Implement this method
    Log.v("LoggerActivity", String.valueOf(requestCode));
    if (requestCode == 2890) {
        Log.v("LoggerActivity", resultCode + ";" + RESULT_OK + ";" + RESULT_CANCELED + ";" + RESULT_FAIL);
        // never reached...
        if (resultCode == RESULT_OK) {
            new CustomToast(MainActivity.this, getLayoutInflater(), "SMS reçu de Zwedge : votre mot de passe est \"" + data.getStringExtra("Pass") + "\"", Toast.LENGTH_LONG, false).show();
        }
        if (resultCode == RESULT_CANCELED) {

        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

SecureKeyActivity

public class SecureKeyActivity extends Activity {

int key;
int keyGot;
String pass;
String pseudo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog_secure_key);
    pass = getIntent().getStringExtra("securePass");
    pseudo = getIntent().getStringExtra("securePseudo");
    keyGot = Common.data.getSecureKey(Common.data.requestForId(pseudo));
    TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4) {
            // TODO: Implement this method
        }
        @Override
        public void onTextChanged(CharSequence p1, int p2, int p3, int p4) {
            try {
                key = Integer.decode(p1.toString());
                if (key == keyGot) {
                    Intent result = getIntent();
                    result.putExtra("Pass", pass);
                    Intent main = new Intent(SecureKeyActivity.this, MainActivity.class);
                    main.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    setResult(RESULT_OK, result);
                    finish();
                    startActivity(main);
                }
            } catch (NumberFormatException e) {}
        }
        @Override
        public void afterTextChanged(Editable p1) {
            // TODO: Implement this method
        }
    };
    ((EditText)findViewById(R.id.insert_key)).addTextChangedListener(watcher);
}

}

所以......我刚刚记录了结果......似乎这些在我的活动中是不一样的...... RESULT_CANCELED = RESULT_OKRESULT_OK = RESULT_CANCELED(首先是MainActivity结果,第二个是SecureKeyActivity结果)

0 个答案:

没有答案