After giving WRITE_EXTERNAL_STORAGE permission RecyclerView is not updating on first installation

I have made a video player app that shows all the folders after user install and give WRITE_EXTERNAL_STORAGE permission UI is not updating i.e folder that are present in device is not showing if i close my app and reopen it all folder are visible .

folder are not visible only in first installation after that it is visible.

here it the code for my HomeActivity.java (MainActivity.java)

public class HomeActivity extends AppCompatActivity {

//    ui
    RecyclerView mRecyclerView;
    TextView titleText;
    ImageView backButton;

    //var
    private static final String TAG = "HomeActivity";
    private static final int REQUEST_CODE_PERMISSION = 123;
    FolderAdapter mFolderAdapter;
    List<String> mFolderList;
    FolderViewModel mFolderViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        titleText = findViewById(R.id.top_folder_name);
        backButton = findViewById(R.id.back_button);
        backButton.setVisibility(View.INVISIBLE);
        mFolderViewModel = new ViewModelProvider(this, new FolderViewModelFactory(this.getApplication(),"new awesome param")).get(FolderViewModel.class);
        Toast.makeText(this,"on create",Toast.LENGTH_SHORT).show();
        mFolderList = new ArrayList<>();
        titleText.setText("Folders");
        permission();

    }

    private void subscribeObservers(){
        mFolderViewModel.getFolders().observe(this, new Observer<List<String>>() {
            @Override
            public void onChanged(List<String> Folders) {
                mFolderAdapter.setFolder(Folders);
            }
        });
    }
    private void initRecyclerView() {
        mRecyclerView = findViewById(R.id.folder_recyclerView);
        mFolderAdapter = new FolderAdapter(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,RecyclerView.VERTICAL,false);
        subscribeObservers();
        mRecyclerView.setAdapter(mFolderAdapter);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        Toast.makeText(HomeActivity.this,"yes",Toast.LENGTH_SHORT).show();

    }


    private void permission() {
        if(ContextCompat.checkSelfPermission(getApplicationContext(),
                Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){

            ActivityCompat.requestPermissions(HomeActivity.this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_CODE_PERMISSION);
        }
        else{
//            Toast.makeText(HomeActivity.this,"first",Toast.LENGTH_SHORT).show();
                //setting observer in recyclerview
            initRecyclerView();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if(requestCode == REQUEST_CODE_PERMISSION){
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
                Toast.makeText(HomeActivity.this, "Permission Granted 2", Toast.LENGTH_SHORT).show();


                //setting observer in recyclerview
                permission();

            }
            else{
                ActivityCompat.requestPermissions(HomeActivity.this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_CODE_PERMISSION);
            }
        }
    }
}

every thing is working ..

thank you in advance.



Read more here: https://stackoverflow.com/questions/64951127/after-giving-write-external-storage-permission-recyclerview-is-not-updating-on-f

Content Attribution

This content was originally published by Dhananjay pathak at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: