0

I have same code in two methods still when I click respective button, the code executes (including finish()), but when I call other method from code it does not. Can someone explain why? How to execute this code without the button's onClick?

@Override
public void onClick(View view) {

    Intent data = new Intent();
    data.putExtra("name", "Adnan");
    setResult(2, data);
    finish();
}

public void qoIntentin(){
    Intent data = new Intent();
    data.putExtra("name", "Adnan");
    setResult(2, data);
    finish();
}
Szymon
  • 41,313
  • 16
  • 90
  • 109
Adnan Pirota
  • 259
  • 5
  • 19
  • Simply instantiate the class this method is in and call it... – wvdz Nov 08 '13 at 22:35
  • If these two methods are both part of the same class then you can just call "qoIntentin()" right from your onClick method. – tiguchi Nov 08 '13 at 22:55
  • It's not working, not even if I instantiate the class which by the way is Android Activity, I don't know if that has anything to do with it.. – Adnan Pirota Nov 08 '13 at 23:23
  • If something is not working then it has a reason that is usually displayed as some sort of error message. Does your code compile? If not then post the compilation / syntax error message. Does your code crash at runtime? Then please post the logcat error message and stack trace here. – tiguchi Nov 08 '13 at 23:45

1 Answers1

1

@Override means that you override a method from your parent class (the one you inherit from by using extends keyword).

Because you're overriding the method from the parent class, this method is called from some code in the parent class or elsewhere, you just provide a different implementation for it.

You can check more in this question and answers: When do you use Java's @Override annotation and why?

Community
  • 1
  • 1
Szymon
  • 41,313
  • 16
  • 90
  • 109
  • Thank you for your answer I know the way override works but I don't know why in this case it is working and other method is not – Adnan Pirota Nov 08 '13 at 23:25
  • @AdnanPirota You're question is a bit unclear and without knowing more code and how you use those methods, it's hard to say so I tried to at least give you some overview. – Szymon Nov 08 '13 at 23:33
  • These two methods belong to a second activity which I call with startActivityforResult from first activity. I have this code from where I want to call my qoIntentin() method: public void aJaKaQellu(){ for (int i = 0 ; i < shkronjatFjales.size(); i++ ){ if (etAt.get(i).getText().toString().toUpperCase().equals(shkronjatFjales.get(i).toString().toUpperCase())){ } else { nukJaKaQellu(); } } displayMessage("E qelluat!"); qoIntentin(); } – Adnan Pirota Nov 08 '13 at 23:41
  • So where exactly do you call `qoIntentin` from? And do I get it right that it doesn't work but `onClick` works? – Szymon Nov 08 '13 at 23:42
  • When user touches the last letter of the word, the method shuold compare if he has guessed the word correctly or not, and if he dit return the intent to other activity, still on my screen when I put the last letter and execute qoIntentin(), it does not work while when I click the button which is on the same screen it works – Adnan Pirota Nov 08 '13 at 23:45