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

蓝咒 提交于 2020-01-03 07:59:06

问题


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 :)


回答1:


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



回答2:


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



回答3:


You should use the onResume() callback on your Activity, which is

Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.



来源:https://stackoverflow.com/questions/11393986/how-to-make-a-callback-after-the-view-is-completely-rendered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!