0

In android I've to achieve following:

If entered password is correct in funToEnterPassword();. How could I know here from which method I called this method so I can continue with functionABC(); or functionXYZ();

public void fun1(){
 funToEnterPassword();
 funcABC();
}


public void fun1(){
 funToEnterPassword();
 functionXYZ();
}


public void funToEnterPassword(){
 //Enter password in popup
 //If password is correct how could I know here from which method I got called this method so I can continue with functionABC() or functionXYZ();
} 
Usman Kurd
  • 7,212
  • 7
  • 57
  • 86
  • If you are calling funToEnterPassword(); in fun1(), then funcABC(); will automatically be called. You dont have to write call for that in funToEnterPassword() – MysticMagicϡ Dec 27 '12 at 06:50
  • u can use static variable , boolean variable or can show a toast – Usman Kurd Dec 27 '12 at 06:50
  • @ShreyaShah, FuncABC() should be called only when entered password is correct and it might be declared out of this function. – Quicklaunch Technology Dec 27 '12 at 06:53
  • You can use printStackTrace method to find out the calling method! StackTraceElement[] stackTraceElements=Thread.currentThread().getStackTrace() refer: (http://stackoverflow.com/questions/421280/in-java-how-do-i-find-the-caller-of-a-method-using-stacktrace-or-reflection) – stack_ved Dec 27 '12 at 06:59

3 Answers3

2

You can use boolean for that either declare method type as boolean or a variable and sets its value as you needed. Simple. :)

Rohit
  • 490
  • 3
  • 15
1

Try following method:

public void fun1(){
   boolean result = funToEnterPassword();
   if (result) 
      funcABC();
}

public void fun2(){
   boolean result = funToEnterPassword();
   if (result)
      functionXYZ();
}


public boolean funToEnterPassword(){

   pwdResult = false;
   //Enter password in popup
   //If correct pwd
   pwdResult = true;
   //If password is correct how could I know here from which method I got called this             method so I can continue with functionABC() or functionXYZ();

   return pwdResult;
}
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
0

Usually the third item in the array should hold the current class and method values! this below code could be of some use!

StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
    if(stackTraceElements != null && stackTraceElements.length > 0){
        if(stackTraceElements.length > 2){
            String methodName = stackTraceElements[2].getMethodName();
            String className = stackTraceElements[2].getClassName();
            Log.e(className, methodName);
            Toast.makeText(this, className + " " + methodName, Toast.LENGTH_LONG).show();
        }

    }
stack_ved
  • 721
  • 3
  • 11