KUNAL AGRAWAL KUNAL AGRAWAL - 1 month ago 11
Android Question

how to switch intent function depend upon activity

how can i switch the function depend upon the parent activity i have two case for which i want to switch between function.my function is related to sms otp verification.

case 1. when user is getting registered function is verifying otp and activating the user.

case2: when user forgot their password they will generate a new otp to reset password.

here is my function code

@Override
protected void onHandleIntent(Intent intent) {

// SqLite database handler
db = new SQLiteHandler(getApplicationContext());
// Fetching user details from sqlite
HashMap<String, String> user = db.getUserDetails();


if (intent != null) {
String otp = intent.getStringExtra("otp");
String phone = user.get("phone");
verifyOtp(otp,phone);

}
}


now i want to make it like

@Override
protected void onHandleIntent(Intent intent) {

// SqLite database handler
db = new SQLiteHandler(getApplicationContext());
// Fetching user details from sqlite
HashMap<String, String> user = db.getUserDetails();


if (intent != null) {
String otp = intent.getStringExtra("otp"); //this coming from sms receiver

//from here i want to switch if parent activity is register activity below function should run


String phone = user.get("phone");
verifyOtp(otp,phone);


// if parent activity if forgot password activity the below function should run

String phone = session.getmobileno();
verifyfpass(otp,phone)

}
}

Answer

If by parent activity you mean from where you are launching the intent, then you can add another parameter within the intent to tell from where the intent has been sent.

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("from", "RegisterActivity");
startActivity(intent);

And then in the handleIntent function, you can just check this variable's value, and perform your function such.

if (intent.hasExtra("from")) {
     String from = intent.getStringExtra("from");
     if (from.equals("RegisterActivity")) {
           //verifyotp
     } else if (from.equals("ForgotParentActivity")) {
          //verifyfpass
     }
}

However, if you want to check the activity you're present in right now, then you can use the instanceof property.

if (this instanceof RegisterActivity) {
     //verifyotp
} else if (this instanceof ForgotParentActivity)) {
     //verifyfpass
}