简体   繁体   中英

How to change this piece of code from (startActivityForResult) to (ActivityResultLauncher) because startActivityForResult is deprecated?

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.

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)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM