Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a callback after the view is completely rendered?

How to make a callback after the view is completely rendered ?

I am trying to call a method which takes a screen-shot of parent view. If I write that code in onCreate() method, the app crashes due to null pointer (as no view is rendered). for now the temporary solution I have implemented is to make a delay of 1 second before calling that method. But, I am looking for a much more robust solution to this problem.

any suggestions and help appreciated. thanks :)

like image 978
jeet.chanchawat Avatar asked Jul 09 '12 11:07

jeet.chanchawat


2 Answers

Try this logic ... always called after the view has got focus or rendered or looses focus

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    doWhateverAfterScreenViewIsRendered();
}
like image 160
Gaurav Chawla Avatar answered Nov 02 '22 15:11

Gaurav Chawla


I have got a better option now :) this really gives a call-back after the layout is rendered

private class LoadActivity extends AsyncTask<String, Void, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        mainMethod();
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        takeScreenShot(1);
    }

    @Override
    protected String doInBackground(String... params) {
        return null;
    };
}

I created the above class and then called this method for execution in onCreate()

LoadActivity loadactivity= new LoadActivity();
loadactivity.execute();
like image 40
jeet.chanchawat Avatar answered Nov 02 '22 15:11

jeet.chanchawat