jeet.chanchawat
jeet.chanchawat

Reputation: 2575

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

Upvotes: 14

Views: 12919

Answers (3)

jeet.chanchawat
jeet.chanchawat

Reputation: 2575

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

Upvotes: 1

Gaurav Chawla
Gaurav Chawla

Reputation: 1847

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

Upvotes: 16

Ovidiu Latcu
Ovidiu Latcu

Reputation: 72331

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.

Upvotes: 0

Related Questions