android / codelab-android-room-with-a-view

Apache License 2.0
746 stars 490 forks source link

Step 16 startActivityForResult has been deprecated #242

Open mikerown7087 opened 1 year ago

mikerown7087 commented 1 year ago

The replacement for startActivityForResult is registerForActivityResult which uses a different interface. As a new app developer (from old school programming) it is not clear how to implement the newer interface.

Kvivek2109 commented 7 months ago

Change your class as: class MainActivity : AppCompatActivity() {

private val wordViewModel: WordViewModel by viewModels {
    WordViewModelFactory((application as WordsApplication).repository)
}
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)
    val adapter = WordListAdapter()
    recyclerView.adapter = adapter
    recyclerView.layoutManager = LinearLayoutManager(this)

    wordViewModel.allWords.observe(this) { words ->
        // Update the cached copy of the words in the adapter.
        words?.let { adapter.submitList(it) }
    }

    val fab = findViewById<FloatingActionButton>(R.id.fab)
    fab.setOnClickListener {
        val intent = Intent(this@MainActivity, NewWordActivity::class.java)
        onActivityResultLauncher.launch(intent)
    }
}

// Declare the activity result launcher
private val onActivityResultLauncher = registerForActivityResult(
    ActivityResultContracts.StartActivityForResult()
) { result ->
    if (result.resultCode == Activity.RESULT_OK) {
        // Handle the result
        val data: Intent? = result.data
        data?.getStringExtra(NewWordActivity.EXTRA_REPLY)?.let {
            val word = Word(it)
            wordViewModel.insert(word)
        }
    } else {
        Toast.makeText(
            applicationContext,
            R.string.empty_not_saved,
            Toast.LENGTH_LONG
        ).show()
    }
}

}

xuantienpham commented 5 months ago

The replacement for startActivityForResult is registerForActivityResult which uses a different interface. As a new app developer (from old school programming) it is not clear how to implement the newer interface.

Change your class (in Java) as class MainActivity : AppCompatActivity() {}

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

    RecyclerView recyclerView = findViewById(R.id.recyclerview);
    final WordListAdapter adapter = new WordListAdapter(new WordListAdapter.WordDiff());
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    mWordViewModel = new ViewModelProvider(this).get(WordViewModel.class);

    mWordViewModel.getAllWords().observe(this, words -> {
        //Update the cached copy of the words in the adapter.
        adapter.submitList(words);
    });

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(view -> {
        Intent intent = new Intent(MainActivity.this, NewWordActivity.class);
        newWordActivityResultLauncher.launch(intent);
    });
}

// Declare the activity result launcher
ActivityResultLauncher<Intent> newWordActivityResultLauncher = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if(result.getResultCode() == Activity.RESULT_OK) {
                    Intent data = result.getData();
                    Word word = new Word(data.getStringExtra(NewWordActivity.EXTRA_REPLY));
                    mWordViewModel.insert(word);
                } else{
                    Toast.makeText(
                            getApplicationContext(),
                            R.string.empty_not_saved,
                            Toast.LENGTH_LONG).show();
                }
            }
        }

);