Delegate between fragment and Activity called but visibility change not triggered
<p>I have a viewpager. It has 3 pages. From activity i have to play some music file when button pressed on fragment. During play, few button will be hide and after play button will be visible. So i use a delegate to communicate between Activity and Fragment. Delegate function fired after play finish but always return to fragment no 0 or 2. Always skip no 1 fragment. So either it enables page 1 or page 3 buttons but not page 2. Please help.</p>

<p>(Sorry for my bad English)</p>

<p>My Interface: </p>

<p>PlayResult.java</p>

<pre><code>public interface PlayResult {
    void PlayCompleted(boolean output);
}
</code></pre>

<p>PlayActivity.java</p>

<pre><code>public PlayResult playDelegate = null;
void play()
{
try
        {
            try {
                if(mp != null &amp;&amp; mp.isPlaying())
                    mp.stop();
                mp = null;
            }
            catch (Exception ex){}
            mp = MediaPlayer.create(PlayActivity.this, StaticClass.getAudioResourceId(fileName, PlayActivity.this));
            mp.start();
            mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    try {
                        mp.stop();
                        mp.release();
                    }
                    catch (Exception ex){}
                    if(playingButton != null)
                    {
                        try {
                            Thread.sleep(1000);
                            playMusic(dm.audio, dm, null, showButton);
                        }
                        catch (Exception ex)
                        {
                            //Log.e("error thread", ex.getMessage());
                        }
                    }
                    else
                    {
                        playDelegate.PlayCompleted(true);
                        //showButton();
                    }



                }
            });
        }
        catch (Exception ex){ //Log.e("error playing", ex.getMessage()+ ex.getCause());

        }

}
</code></pre>

<p>and in my fragment</p>

<p>viewpager_fragment.java</p>

<pre><code>PlayActivity playActivity;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            view = inflater.inflate((R.layout.viewpager_layout), container, false);
            _context = view.getContext();

           imgButton1 = (ImageView) view.findViewById(R.id.imgButton1);

           imgButton1.setOnClickListener(new View.OnClickListener() {
            @Override
             public void onClick(View v) {
               playMusic(dm.b1audio, dm, imgButton1,imgButton1);
             }
           });
         imgButton2 = (ImageView) view.findViewById(R.id.imgButton2);

         imgButton2.setOnClickListener(new View.OnClickListener() {
         @Override
              public void onClick(View v) {
                 playMusic(dm.b2audio, dm, imgButton2,imgButton2);
          }
         });

        img = (ImageView) view.findViewById(R.id.image);
        img.setImageResource(StaticClass.getResourceId(dm.image, _context));


             btnMoveForward = (ImageView) view.findViewById(R.id.btnMoveForward);
                playActivity = (PlayActivity) getActivity();
                playActivity.playDelegate = this;


         }



     private void showButton()
        {
            btnMoveBackward.setVisibility(View.VISIBLE);
            btnMoveForward.setVisibility(View.VISIBLE);
            btnHome.setVisibility(View.VISIBLE);

            imgButton1.setVisibility(View.VISIBLE);
            imgButton2.setVisibility(View.VISIBLE);
            imgButton1.setEnabled(true);
            imgButton2.setEnabled(true);

        }
     void playMusic(String fileName, final DataModel dm , final ImageView playingButton, final ImageView showButton)
        {
            hideButton(showButton);
            playActivity.playMusic(fileName, dm, playingButton, showButton);
        }

        @Override
        public void PlayCompleted(boolean output) {

            showButton();
            Log.e("Play completed del", "from delegate"+ ", dm "+dm.pageno+ btnHome.getVisibility());
        }
</code></pre>