In order to learn whether the verification was successfully completed or cancelled we need to set a callback. There are 2 options to set up a callback

  • override onActivityResult
  • using ActivityResultLauncher

Overriding onActivityResult

In this case we simply need to add the onActivityResult method as shown in the code snippet

class MainActivity : AppCompatActivity() {
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        //..

        val button = findViewById<MetamapButton>(R.id.metamapButton)
        button.setParams(this, "YOUR_CLIENT_ID", "YOUR_FLOW_ID")
    }
    
     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == MetamapSdk.DEFAULT_REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK && data != null) {
                // There are no request codes
                Toast.makeText(
                    this,
                    "onActivityResult Verification success! " +
                            "VerificationId: ${data.getStringExtra(MetamapSdk.ARG_VERIFICATION_ID)}, " +
                            "IdentityId: ${data.getStringExtra(MetamapSdk.ARG_IDENTITY_ID)}",
                    Toast.LENGTH_SHORT
                ).show()
            } else {
                if (data != null) {
                    Toast.makeText(
                        this,
                        "onActivityResult Verification cancelled! " +
                                "VerificationId: ${data.getStringExtra(MetamapSdk.ARG_VERIFICATION_ID)}, " +
                                "IdentityId: ${data.getStringExtra(MetamapSdk.ARG_IDENTITY_ID)}",
                        Toast.LENGTH_SHORT
                    ).show()
                } else {
                    Toast.makeText(
                        this,
                        "onActivityResult Verification cancelled!",
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data)
        }
    }
}
public class MainActivity extends AppCompatActivity {

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

        //..
      
        MetamapButton button = findViewById(R.id.metamapButton);
        button.setParams(this, "CLIENT_ID", "FLOW_ID");
    }
  
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MetamapSdk.DEFAULT_REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK && data != null) {
                // There are no request codes
                Toast.makeText(
                        this,
                        "onActivityResult Verification success! " +
                                "VerificationId: ${data.getStringExtra(MetamapSdk.ARG_VERIFICATION_ID)}, " +
                                "IdentityId: ${data.getStringExtra(MetamapSdk.ARG_IDENTITY_ID)}",
                        Toast.LENGTH_SHORT
                ).show();
            } else {
                if (data != null) {
                    Toast.makeText(
                            this,
                            "onActivityResult Verification cancelled! " +
                                    "VerificationId: ${data.getStringExtra(MetamapSdk.ARG_VERIFICATION_ID)}, " +
                                    "IdentityId: ${data.getStringExtra(MetamapSdk.ARG_IDENTITY_ID)}",
                            Toast.LENGTH_SHORT
                    ).show();
                } else {
                    Toast.makeText(
                            this,
                            "onActivityResult Verification cancelled!",
                            Toast.LENGTH_SHORT
                    ).show();
                }
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}

Using ActivityResultLauncher

Depending on how we start the Metapam SDK, we need to add activityResultLauncher to

  • either MetamapButton's setParams(...)
  • or to MetamapSdk.startFlow(...)
class MainActivity : AppCompatActivity() {
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        //..

        val button = findViewById<MetamapButton>(R.id.metamapButton)
        button.setParams(activityResultLauncher, this, "YOUR_CLIENT_ID", "YOUR_FLOW_ID")
    }

    private val activityResultLauncher =
                registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
                    val data = result.data
                    if (data == null) {
                        Toast.makeText(requireContext(), "Verification cancelled", Toast.LENGTH_SHORT)
                            .show()
                        return@registerForActivityResult
                    }
                    if (result.resultCode == Activity.RESULT_OK) {
                        // There are no request codes
                        Toast.makeText(
                            requireContext(),
                            "Verification success! " +
                                    "VerificationId: ${data.getStringExtra(MetamapSdk.ARG_VERIFICATION_ID)}, " +
                                    "IdentityId: ${data.getStringExtra(MetamapSdk.ARG_IDENTITY_ID)}",
                            Toast.LENGTH_SHORT
                        ).show()
                    } else {
                        Toast.makeText(
                            requireContext(),
                            "Verification cancelled! " +
                                    "VerificationId: ${data.getStringExtra(MetamapSdk.ARG_VERIFICATION_ID)}, " +
                                    "IdentityId: ${data.getStringExtra(MetamapSdk.ARG_IDENTITY_ID)}",
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                }
}
public class MainActivity extends AppCompatActivity {

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

        //..
      
        MetamapButton button = findViewById(R.id.metamapButton);
        button.setParams(activityResultLauncher, this, "CLIENT_ID", "FLOW_ID");
    }

    private ActivityResultLauncher<Intent> activityResultLauncher = registerForActivityResult(
                    new ActivityResultContracts.StartActivityForResult(),
                    result -> {
                        Intent data = result.getData();
                        if (data == null) {
                            Toast.makeText(MainActivity.this, "Verification cancelled", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (result.getResultCode() == Activity.RESULT_OK) {
                            // There are no request codes
                            Toast.makeText(
                                    MainActivity.this,
                                    "Verification success! " +
                                            "VerificationId:" + data.getStringExtra(MetamapSdk.ARG_VERIFICATION_ID) +
                                            "IdentityId: " + data.getStringExtra(MetamapSdk.ARG_IDENTITY_ID),
                                    Toast.LENGTH_SHORT
                            ).show();
                        } else {
                            Toast.makeText(
                                    MainActivity.this,
                                    "Verification cancelled! " +
                                            "VerificationId:" + data.getStringExtra(MetamapSdk.ARG_VERIFICATION_ID) +
                                            "IdentityId: " + data.getStringExtra(MetamapSdk.ARG_IDENTITY_ID),
                                    Toast.LENGTH_SHORT
                            ).show();
                        }
                    });
}

What’s Next

Having Troubles? Please visit Troubleshoot section or check out our demo apps.