Question

I have a problem by calling a method. I created a button, if I click on it, it start the onClick(). This method is calling to other methods from up-class. In these methods I set the layout by setContentView(R.layout...);to two different layouts. At the moment my code change the layout very fast to the second layout. How can I handle it, that the second layout will only set, if the first method is ready? I know it must be something with asynch-tasks, but I need help to handle it.

My code:

public void do(int a) {
 method1();
 method2();
 }

Both methods are from an superclass. Method1 should setContentView(r.layout.1) and method2 to r.layout.2.

Was it helpful?

Solution

if you want some time between first and second method you can call second method after some delay using handler as below

public void do(int a) {
 method1();
new Handler().postDelayed(new Runnable() 
{

        @Override
        public void run() 
                {
            // TODO Auto-generated method stub
            try
            {
                method2();
            }
            catch(Exception e)
            {

                           Log.d("Exception",""+e.getMessage());
            }
        }
        }, 500);

 }

OTHER TIPS

You can do an AsyncTask:

public class BasicAsyncTask extends AsyncTask{

    @Override
    protected Object doInBackground(Object[] params) { // this will start when .execute() is called
        method1();
        return null;
    }

    @Override
    protected void onPostExecute(Object o) {   // this will be executed when doInbackGround finishes
        super.onPostExecute(o);
        method2();
    }
}

to start AsynkTask:

BasicAsyncTask basicAsyncTask = new BasicAsyncTask();
basicAsyncTask.execute();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top