1

I am trying to fetch contacts and show it in a list. I am using RxPermissions for that. The problem here is when I first allow the permission the view returns null and shows empty list even though the list has items in it. But when I go back to the previous activity and come back it works fine.

ContactsFragment.kt

override fun onResume() {
        super.onResume()
        if (isPermissionGranted) {
            Timber.d("Permission is granted")
            contactsPresenter.fetchContacts(context)
            noPermissionsWarning.visibility = View.GONE
        } else {
            noPermissionsWarning.visibility = View.VISIBLE
            showContactList(ArrayList()) // Show empty list when permissions not granted
        }
    }
private fun askPermissions() {
    rxPermissions
            .request(android.Manifest.permission.GET_ACCOUNTS,
                    android.Manifest.permission.READ_CONTACTS,
                    android.Manifest.permission.WRITE_CONTACTS)
            .subscribe { permissionGranted ->
                isPermissionGranted = permissionGranted
            }
}



override fun showContactList(selectedContacts: ArrayList<Contact>) {
        contactAdapter = ContactListAdapter(selectedContacts, this, context)
        val layoutManager = LinearLayoutManager(this.context)
        contactsRecyclerView?.layoutManager = layoutManager
        contactsRecyclerView?.addItemDecoration(DividerItemDecoration(
                contactsRecyclerView?.context,
                DividerItemDecoration.VERTICAL
        ))
        contactsRecyclerView?.adapter = contactAdapter
    }

ContactsPresenter.kt

override fun takeView(view: ContactsContract.View, context: Context?) {
        super.takeView(view)
        contactList.clear()
        tempContactList.clear()
    }

    override fun fetchContacts(context: Context?) {
        contactManager.getContacts(context)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    it.let {
                        tempContactList.addAll(it.sortedWith(compareBy { it.userName }))
                        contactList.addAll(it.sortedWith(compareBy { it.userName }))
                        view?.showContactList(contactList)// this view is null at first
                    }
                }, {

                }).addDisposableTo(disposable)
    }

What would I be missing here.

Thank you

Nejweti
  • 113
  • 2
  • 11
  • i have a answer for this in java but the problem is you have to call your contactFetching method in both places..... 1. when you check is permission granted if it is not granted to call the method for permission and also put your fetching method here may be you don't get this so please ask me again – Vipul Chauhan Mar 14 '19 at 06:59
  • Now check the answer.... – Vipul Chauhan Mar 14 '19 at 07:55

3 Answers3

2

It looks like you should be fetching your contacts right after this line:

isPermissionGranted = permissionGranted
Gavin Wright
  • 3,124
  • 3
  • 14
  • 35
1

call your method when you check for permission granted or not

 if (ContextCompat.checkSelfPermission(MainActivity.this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) == 
                      PackageManager.PERMISSION_GRANTED) {
Filtering filtering=new Filtering();
//contactFetch method  here
    } else {         

     Permission.requestStoragePermission(MainActivity.this,
                    STORAGE_PERMISSION_CODE);
    }

After that when you are asking for permission......

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == STORAGE_PERMISSION_CODE)  {
        if (grantResults.length > 0 && grantResults[0] == 
                   PackageManager.PERMISSION_GRANTED) {
            Filtering filtering=new Filtering();
            //Again your fetching method
        } else {
            Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();
            Permission.requestStoragePermission(MainActivity.this,
                                  STORAGE_PERMISSION_CODE);
        }
    }
}

Try this i am not sure but i think it will help you

Vipul Chauhan
  • 189
  • 4
  • 19
0

I solved it by attaching the view both inside onResume() and onViewCreated(). Thanks everyone for the help

  • After quite a long time I figured out that I was clearing the disposable on onPause() which should have been done inside onDestroy() in my case. I wrote the solution in case it helps some one
Nejweti
  • 113
  • 2
  • 11