Click here to Skip to main content
15,885,878 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I within Android, if a function is called which is already running, cancel/stop that function then start it from the beginning?

My code looks like the below, basically throughout my application a given function( Important(); ) is called but rather than have that function run several times. I'd like to Cancel/stop it, then start it from the beginning?

How can I go about doing this?

Code:
Java
public void Important() {
//Do lots of stuff
}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Important();
    }

    @Override
    protected void onResume() {
        super.onResume();
    Important();
    }


I've already attempted the below making use of booleans. But it just stops a user from running the function again if already running? (which isn't what I'm trying to do)

I want, when it is called again. The running function is stopped. Then freshly run it

Code:

Java
Boolean runningCode = false;
    public void Important() {
if (runningCode== false) {
runningCode = true;
    //Do lots of stuff
runningCode = false;
    }


F.Y.I

Important() is called many times throughout my application, sometimes whilst Important() is still running. Hence the need for a solution to my question :-)


Tasks like resetting variables, formatting the page are carried out within Important();, it is not running on a separate thread. These are tasks that need to be completed on the main UI thread
Posted
Updated 21-Nov-14 0:59am
v5
Comments
Richard MacCutchan 21-Nov-14 8:42am    
If it's not running on a separate thread then there is no issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900