记住调用下一个方法的上一个方法

时间:2012-12-27 06:42:37

标签: android

在android中我要实现以下目标:

如果在funToEnterPassword();中输入的密码正确无误。我怎么知道这个方法我称之为这种方法,所以我可以继续functionABC();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();
} 

3 个答案:

答案 0 :(得分:2)

您可以使用布尔值将声明方法类型声明为布尔值或变量,并根据需要设置其值。简单。 :)

答案 1 :(得分:1)

尝试以下方法:

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;
}

答案 2 :(得分:0)

通常数组中的第三项应该包含当前的类和方法值!以下代码可能有用!

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();
        }

    }