Skip to main content

Simplify Your Tax Calculations with GST Calculator 2024: The Ultimate Tool for Businesses and Individuals

OnActivityResult method is deprecated, you are looking for an alternative? Here It is the Latest Solution in android | Kotlin

 One of the most used method OnActivityResult is deprecated in android. Here it is the alternative solution you can follow-




In First Activity, take val resultLauncher using below code-

private val resultLauncher = registerForActivityResult(StartActivityForResult()) { result ->
        if (result.resultCode == Activity.RESULT_OK) {
            

            try {
                val data: Intent? = result.data

                
                val stringResponse = data?.getStringExtra("response")!!
} catch (e: Exception) { e.printStackTrace() } } }


Now Call this val, where you want to make startActivityFor Result
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        button.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            resultLauncher.launch(intent)
        }
    }



In second Activity return the value-

    private fun successResult(returnString: String) {
        val intent: Intent = intent
        intent.putExtra("response", returnString)
setResult(Activity.RESULT_OK, intent) finish() } private fun cancelResult() { val intent: Intent = intent setResult(Activity.RESULT_CANCELED, intent) finish() }



Thanks,

Comments

  1. This is a well-researched and informative piece. I appreciate the depth of information and the clarity of your writing. Great job!
    Best Embedded Training in chennai
    Best Embedded Training in coimbatore

    ReplyDelete
    Replies
    1. Thank you so much for your kind words! I'm thrilled to hear that you found the piece well-researched and informative. Your feedback means a lot to me and motivates me to keep delivering quality content.

      Delete

Post a Comment

Popular posts from this blog

How to Insert Multiple rows in a single db transaction in Android Room Database? | Android | Room DB

  To insert multiple rows into a Room database in Android, you can follow these steps: 1. Set up Room Database: First, make sure you have set up your Room database correctly in your Android project. Define your Entity class, create a Database class that extends RoomDatabase, and set up your DAO (Data Access Object) interface. 2. Create Entity Class: Define an Entity class that represents the data you want to insert into the database. For example: 1 2 3 4 5 6 7 8 9 @Entity (tableName = "my_table" ) public class MyEntity { @PrimaryKey (autoGenerate = true ) public int id; public String name; public int age; // Add other fields and getters/setters as needed } Create DAO: Create a DAO interface with a method to insert multiple rows. For example: 1 2 3 4 5 @Dao public interface MyEntityDao { @Insert void insertAll (List<MyEntity> entities); } Initialise Database and DAO: In your application code, create an insta...

How to fetch Latitude, Longitude from address and vice-versa(address from Latitude, Longitude) using Google Geo coder SDK in android| Kotlin

 In this Android development related article, you will get a simple solution that, how to get address using Latitude, Longitude and vice-versa. i.e latitude, longitude from an address text. It is very easy and simple. Read full article and carefully follow all the steps. Here we use google Geocoder SDK. Okay, first we create an android project in kotlin and create an Activity say MainActivity.kt. Use the below code- Function get Latitude, Longitude from Address- fun getLatLngFromAddress (context: Context, mAddress: String): String { val coder = Geocoder(context) lateinit var address: List<Address> try { address = coder.getFromLocationName(mAddress, 5 ) if (address == null ) { return "Fail to find Lat,Lng" } val location = address[ 0 ] return " Latitude: ${location.latitude}\n Longitude: ${location.longitude}" } catch (e: Exception...

onBackPressed() method is deprecated, you are looking for an alternative? Here It is the Latest Solution in android | Kotlin

With the evolution of Android development, several APIs and functions get deprecated over time to make way for more robust and flexible alternatives. One such deprecation that developers need to be aware of is the onBackPressed() method in Android. In Android, onBackPressed() was commonly used to handle back button presses within an activity. However, with the introduction of the Jetpack libraries and the emphasis on more structured navigation, this method has been deprecated. This blog will explore why onBackPressed() is deprecated and what the best alternative solutions are in Kotlin for handling back navigation. Why onBackPressed() is Deprecated The primary reasons for deprecating onBackPressed() include: Improved Navigation Architecture: Android Jetpack's Navigation Component provides a more consistent and predictable way to handle navigation, including back navigation. Lifecycle Awareness: Handling back...