Android: Stopping java main method without closing the activity

86 views Asked by At

For Android, I have programmed an App in which a String is given and it is cut into smaller Strings and saved in a String array always after a hyphen. This only happens once a button is pressed and everything works in case the given string is in the correct format.

In case someone has given the wrong input (maybe without hyphens or so), an exception occurs. I would like to handle the exception in such a way, that just the java code stops at this point and no code is being executed until the button is pressed again?

Is there a way of doing this, as system.exit() closes everything completely and I don't know any alternative...

Apart from this special case, it would be nice to know if this is possible at all. Like, stop the method once some defined int variable becomes -10 or so...

Thanks in advance for your help

1

There are 1 answers

1
janithcooray On BEST ANSWER

I recommend using try-catch exception handlers. but if you want the stop execution on the activity ,use this when you want the activity to stop :

finish();

or use return to stop a method without closing the activity

return;

if your app returns something use:

return null;