Home > Net >  How to change this piece of code from (startActivityForResult) to (ActivityResultLauncher) because s
How to change this piece of code from (startActivityForResult) to (ActivityResultLauncher) because s

Time:07-24

I know there are some other ways to update RecyclerView like restarting the activity or putting update code inside onResume(); but none of these two ways are efficient, any how. I've used startActivityForResult(intent, 1); but now it is deprecated, I want to use ActivityResultLauncher<Intent> startForResult. Here is the deprecated code which is working.

Inside MainActivity

fabNote.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(MainActivity.this, AddNote.class);
            startActivityForResult(intent, 1);

        }
    });

inside AddNote class:

 Intent intent = new Intent();
            setResult(RESULT_OK,intent);
            finish();

Inside MainActivity:

 @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode== RESULT_OK){
        dbHelper = new DbHelper(this);
        allItems = dbHelper.getNotesAndChecklists();
        if(requestCode==1){
            if(data != null){
                noteAdapter.update(allItems);
            }
        }
    }
}

How to change this code to work with the method ActivityResultLauncher<Intent>?

I don't have anything to return and put it in putExtra , all I want is when the note added to SQLite Database and when RecyclerView gets updated when return back to main Activity. As I said I can use this way to update RecyclerView

 @Override
protected void onResume() {
    super.onResume();
    noteAdapter.update(allItems);
}

but let's say the user pressed the home button and then return to the app at this time onResume gets called and RecyclerView is updated though it is unnecessary to update in this case because the data isn't changed.

CodePudding user response:

You must enter the definition of your ActivityResultLauncher in the onCreate() method of your activity.

activityResultLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (result.getResultCode() == Activity.RESULT_OK) {
                        dbHelper = new DbHelper(this);
                        allItems = dbHelper.getNotesAndChecklists();
                        
                        if(result.getData() != null)
                            noteAdapter.update(allItems);
                    }
                });

Also, add your ActivityResultLauncher variable as a private attribute of the activity.

private ActivityResultLauncher<Intent> activityResultLauncher;

After that it starts the activity through it.

fabNote.setOnClickListener((View.OnClickListener) view -> activityResultLauncher
                .launch(new Intent(MainActivity.this, AddNote.class)));
  • Related