John Ballinger
John Ballinger

Reputation: 7550

Android Fragment - Detect if view is active/visible

Refresh code

   public void viewRefresh(){
        getProgressBar(R.id.spinnerBar).setAlpha(1);  // CRASH HERE. FATAL EXCEPTION: main
java.lang.NullPointerException

On Resume Code

  public void onResume(){
        super.onResume();
        viewRefresh();


   API.getInstance().bidOnAuction(auctionID, bid, new JsonHttpResponseHandler(){

                    @Override  // this code is calls refresh after 3 or so seconds.
                    public void onSuccess(JSONObject results) {
                        // BID Successfully
                        Log.i("BID ", "SUCCESS");
                        viewRefresh();

I have an android app API 19. I have some async code that cools a redraw. What I have found that if I load the fragment, then before the callback has occurred, I pop the fragment. The view is gone but the code keeps executing. And therefore. I cannot get access to my Progressbar.

How do detect if my fragment view is still active? (without complex fragment tagging logic?)

Upvotes: 1

Views: 967

Answers (1)

Piovezan
Piovezan

Reputation: 3223

The Fragment class has methods for that checking, such as isAdded(), isDetached(), etc.

Fragment.isAdded() probably will work for you.

Upvotes: 1

Related Questions