arvin99
arvin99

Reputation: 65

How to execute android function sequentially?

I make a game about 8Puzzle on android with AI using A* algorithm. Everything works fine but there is a problem, there are some function that is executed in parallel. What I want is the function is executed after another function is finished.

Here is code:

if(AItype.equals("A*"))
{
    DisableButton();
    DisableClickImageView();
    AStarSolver as = new AStarSolver();
    as.solvePuzzle(arr, GOAL); //Solve the puzzle
    displayResult(as.solutionPath); //display animation
    as = null;

    Toast.makeText(getApplicationContext(), "Finished", Toast.LENGTH_LONG).show();
    copySTARTtoArray();
    setImageResource();
    EnableButton();             
}

I want text "Finished" is displayed after function displayResult() is finished but the text "Finished" show at the same time with function displayResult().

How to solve this??

Edit:

Here is the code for displayResult();

public void displayResult(final Stack<Node> solutionPath)
{
    Handler handler = new Handler(); 
    for (int i = 0; i < solutionPath.size(); i++)
    {
        handler.postDelayed(new Runnable() { 
            public void run() { 
                    Node solNode = solutionPath.pop();
                    //solNode.NodeState.
                    tile00.setImageResource(solNode.getImageResourceBasedNodeState(0));
                    tile01.setImageResource(solNode.getImageResourceBasedNodeState(1));
                    tile02.setImageResource(solNode.getImageResourceBasedNodeState(2));
                    tile10.setImageResource(solNode.getImageResourceBasedNodeState(3));
                    tile11.setImageResource(solNode.getImageResourceBasedNodeState(4));
                        tile12.setImageResource(solNode.getImageResourceBasedNodeState(5));
                    tile20.setImageResource(solNode.getImageResourceBasedNodeState(6));
                        tile21.setImageResource(solNode.getImageResourceBasedNodeState(7));
                    tile22.setImageResource(solNode.getImageResourceBasedNodeState(8));  
            } 
        }, 800 * (i + 1));  

    }   
}

It display the result of animation (Ex: tile0 to tile1)

Upvotes: 0

Views: 1493

Answers (3)

Pankaj Arora
Pankaj Arora

Reputation: 10274

function(){
//code of this function

 function1();
}
function1(){
//code of this function

 function2();
}


function2(){
}

Upvotes: 0

Adnan
Adnan

Reputation: 5075

Try this.

public void displayResult(final Stack<Node> solutionPath)
{
    Handler handler = new Handler(); 
    for (int i = 0; i < solutionPath.size(); i++)
    {
        handler.postDelayed(new Runnable() { 
            public void run() { 
                    Node solNode = solutionPath.pop();
                    //solNode.NodeState.
                    tile00.setImageResource(solNode.getImageResourceBasedNodeState(0));
                    .
                    .
                    .
                    tile22.setImageResource(solNode.getImageResourceBasedNodeState(8));  
            } 
        }, 800 * (i + 1));  

    }  
// Put following code
 handler.postDelayed(
    new Runnable() { 
      public void run() { 
       Toast.makeText(getApplicationContext(), "Finished", Toast.LENGTH_LONG).show(); 
     }
     },800*(solutionPath.size()+1)
    );

}

Upvotes: 1

Toris
Toris

Reputation: 2386

Your code should be look like this.

solvePuzzle(...){
    // solved #1
    displayResult(...){
    }

    // solved #2
    displayResult(...){
    }
}

or use thread.


as.solvePuzzle(arr, GOAL); //Solve the puzzle
displayResult(as.solutionPath); //display animation

means solvePuzzle is all done, then call displayResult.

Upvotes: 0

Related Questions