skip to Main Content

how to solve this kind of problem starActivityForResult please any budy guide me to solve this

2

Answers


  1. startActivityForResult is deprecated, meaning that it will no longer be supported going forward. That is why Android Studio is showing you the lines through the code. Instead, review this Stackoverflow post on alternative options:
    OnActivityResult method is deprecated, what is the alternative?

    Login or Signup to reply.
  2. startActivityForResult is deprecated following is new the way. Here is a full source code example

    MainActivity

    class MainActivity : AppCompatActivity() {
    
        private lateinit var tvResult:TextView
        private lateinit var btnNext:Button
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            tvResult = findViewById(R.id.tv_result)
            btnNext = findViewById(R.id.btn_next)
    
            btnNext.setOnClickListener {
                openActivityForResult(Intent(this,SampleActivity::class.java))
            }
    
        }
    
        private var resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            if (result.resultCode == Activity.RESULT_OK) {
                val mIntent: Intent? = result.data
                val mDataString = mIntent?.getStringExtra("result_data")
                tvResult.text = mDataString
    
            }
        }
    
        private fun openActivityForResult(mIntent: Intent) {
            resultLauncher.launch(mIntent)
        }
    }
    

    SampleActivity

    class SampleActivity : AppCompatActivity() {
    
        private lateinit var btnDone: Button
        private val sampleData = "Sample Activity Result Data"
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_sample)
    
            btnDone = findViewById(R.id.btn_done)
            btnDone.setOnClickListener{
                returnForResult()
            }
        }
    
        private fun returnForResult(){
            val replyIntent = Intent()
            replyIntent.putExtra(
                "result_data",
                sampleData
            )
            setResult(Activity.RESULT_OK, replyIntent)
            finish()
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search