androidfragmentpageradapter

How to get the current item inside FragmentPagerAdapter?


I have the following code -

@Override
  public void sendDeviceIdResult(String deviceId, boolean isAlreadyExist) {

    int currentItem = viewPager.getCurrentItem();
    Fragment item = adapter.getItem(currentItem);
    if (item instanceof PhoneStateAndAgeVerificationFragment) {
      Dialog dialog = ((PhoneStateAndAgeVerificationFragment) item).getProgressDialog();
      if (dialog != null && dialog.isShowing()) {
        dialog.dismiss();
      }
    }

    Intent intent = new Intent();
    intent.putExtra(KEY_DEVICE_ID, deviceId);
    intent.putExtra(KEY_IS_SUCCESS, !isAlreadyExist);
    setResult(RESULT_OK, intent);
    finish();


  }

The issue is that one the line of getItem(position) it does not get the actual reference of the current fragment, but creates a brand new one.

I want to get the reference to the current one and dismiss it's AlertDialog on it.

I know this has been asked many times but I got completely lost while reading all the solutions, so if someone could please show me something that is relevant to my case?

here is my PhoneStateAndAgeVerificationFragment -


package com.onemdtalent.app.ui.verification;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.core.content.ContextCompat;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.onemdtalent.app.App;
import com.onemdtalent.app.R;
import com.onemdtalent.app.core.base.AbstractFragment;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

public class PhoneStateAndAgeVerificationFragment extends AbstractFragment{
    @BindView(R.id.frag_verification_phone_checkbox_age)
    CheckBox checkBoxAge;
    private AlertDialog.Builder mBuilder;
    private Dialog mDialog;


    public static Fragment newInstance() {
        return new PhoneStateAndAgeVerificationFragment();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mBuilder = new AlertDialog.Builder(getActivity(), R.style.DialogTheme);
    }

    @OnCheckedChanged(R.id.frag_verification_phone_checkbox_age)
    void OnAgeCheckedChanged(CompoundButton button, boolean checked) {
        if (checked) {
            button.setTextColor(ContextCompat.getColor(button.getContext(), R.color.black));
        }
    }

    @OnClick(R.id.verification_button_got_it)
    void onBtnGotItClicked(View view) {
        if (!checkBoxAge.isChecked()) {
            checkBoxAge.setTextColor(ContextCompat.getColor(checkBoxAge.getContext(), R.color.accent_red));
            return;
        }
        showProgressDialog();
        if (getContext() instanceof VerificationPageListener) {
            ((VerificationPageListener) getContext()).onPageAgreed();
        }
    }
    @Override
    protected int getLayoutResourceId() {
        return R.layout.layout_verification_phone_age;
    }


    private void showProgressDialog(){
        if (mBuilder == null) {
            mBuilder = new AlertDialog.Builder(App.getAppContext());
        }
        mBuilder.setCancelable(false);
        mBuilder.setView(R.layout.custom_proggress_dialog);
        mDialog = mBuilder.create();
        mDialog.show();
    }

    public Dialog getProgressDialog() {
        return mDialog;
    }
}



Solution

  • Try adding the following code in your FragmentPagerAdapter, this has worked for me.

    public class MyPagerAdapter extends FragmentPagerAdapter {
    
        private Fragment mCurrentFragment;
    
        public Fragment getCurrentFragment() {
            return mCurrentFragment;
        }
    
        @Override
        public void setPrimaryItem(ViewGroup container, int position, Object object) {
            if (getCurrentFragment() != object) {
                mCurrentFragment = ((Fragment) object);
            }
            super.setPrimaryItem(container, position, object);
        }
    }
    

    Call the getCurrentFragment() method from your sendDeviceIdResult() method

    I hope this helps.