Search code examples
androidandroid-asynctasksuperclasssetcontentview

Put method to AsyncTask


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.


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);
    
     }