OnActivityResult method is deprecated, what is the alternative?

前端 未结 5 537
情话喂你
情话喂你 2021-01-01 10:33

Recently I faced the onActivtyResult is deprecated. what should we do for handle it?

any alternative introduced for that?

相关标签:
5条回答
  • 2021-01-01 10:44

    In KOTLIN I changed my code

    startActivityForResult(intent, Constants.MY_CODE_REQUEST)
    

    and

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                Constants.MY_CODE_REQUEST -> {
                ...
    }
    

    FOR

    registerForActivityResult(StartActivityForResult()) { result ->
        onActivityResult(Constants.MY_CODE_REQUEST, result)
    }.launch(intent)
    

    and

    private fun onActivityResult(requestCode: Int, result: ActivityResult) {
        if(result.resultCode == Activity.RESULT_OK) {
            val intent = result.data
            when (requestCode) {
                Constants.MY_CODE_REQUEST -> {
                ...
    

    I hope it works for you. :D

    0 讨论(0)
  • 2021-01-01 10:48

    From now, startActivityForResult() has been deprecated so use new method instead of that.

    Kotlin Example

    fun openActivityForResult() {
             startForResult.launch(Intent(this, AnotherActivity::class.java))
    }
    
    
    val startForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { 
    result: ActivityResult ->
        if (result.resultCode == Activity.RESULT_OK) {
            val intent = result.data
            // Handle the Intent
            //do stuff here
        }
    }

    0 讨论(0)
  • 2021-01-01 10:52

    onActivityResult, startActivityForResult, requestPermissions, and onRequestPermissionsResult are deprecated on androidx.fragment from 1.3.0-alpha04, not on android.app.Activity.
    Instead, you can use Activity Result APIs with registerForActivityResult.

    0 讨论(0)
  • 2021-01-01 10:59

    It seems that onActivityResult is deprecated in the super class but you did not mention the super class name and compileSdkVersion here in your question.

    In Java and Kotlin every class or method could be marked as deprecated simply by adding @Deprecated to it so check your super class you may extend a wrong class.

    When a class is deprecated all of its methods are deprecated too.

    To see a quick solution click on deprecated method and press Ctrl+Q in Android studio to see documentation of method there should be a solution for it.


    In my project using androidx and API 29 as compileSdkVersion, this method is NOT deprecated in activities and fragments

    0 讨论(0)
  • 2021-01-01 11:01

    A basic training is available at developer.android.com.

    Here is an example on how to convert the existing code with the new one:

    The old way:

        public void openSomeActivityForResult() {
            Intent intent = new Intent(this, SomeActivity.class);
            startActivityForResult(intent, 123);
        }
    
        @Override
        protected void onActivityResult (int requestCode, int resultCode, Intent data) {
            if (resultCode == Activity.RESULT_OK && requestCode == 123) {
                doSomeOperations();
            }
        }
    

    The new way (Java):

        // You can do the assignment inside onAttach or onCreate, i.e, before the activity is displayed
        ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        if (result.getResultCode() == Activity.RESULT_OK) {
                            // There are no request codes
                            Intent data = result.getData();
                            doSomeOperations();
                        }
                    }
                });
    
        public void openSomeActivityForResult() {
            Intent intent = new Intent(this, SomeActivity.class);
            someActivityResultLauncher.launch(intent);
        }
    

    The new way (Kotlin):

    var resultLauncher = registerForActivityResult(StartActivityForResult()) { result ->
        if (result.resultCode === Activity.RESULT_OK) {
            // There are no request codes
            val data: Intent? = result.data
            doSomeOperations()
        }
    }
    
    fun openSomeActivityForResult() {
        val intent = Intent(this, SomeActivity::class.java)
        resultLauncher.launch(intent)
    }
    
    0 讨论(0)
提交回复
热议问题