Home > Blockchain >  Intent with string as activity in Kotlin
Intent with string as activity in Kotlin

Time:01-17

I'm trying to launch a activity based on whats in my sharedpreference string, so that I can use 1 login page that redirects to the corresponding activity based on the button I click. Here I put in the sharedpref what the activity is called, which is ConfigureActivity.

 sharedSettingsData.edit()
                .putString("settingsLogin", "ConfigureActivity")
                .apply()

Then here I get whats inside the sharedpref and insert it into a variable:

 val activityHandler = sharedSettingsData.getString("settingsLogin","").toString()

and here I try to launch it:

 val intent = Intent(this, activityHandler::class.java)
            startActivity(intent)

But when doing this I get this error:

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.myapp/java.lang.String};

CodePudding user response:

I fixed my problem using this:

          try {
                if(sharedSettingsData.getString("settingsLogin", "") == "ConfigureActivity"){
                    val i = Intent(this, ConfigureActivity::class.java)
                    startActivity(i)
                    overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left)
                }
                if(sharedSettingsData.getString("settingsLogin", "") == "SettingsChangePin"){
                    val i = Intent(this, SettingsChangePin::class.java)
                    startActivity(i)
                    overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left)
                }
                if(sharedSettingsData.getString("settingsLogin", "") == "ConnectionsActivity"){
                    val i = Intent(this, ConnectionsActivity::class.java)
                    startActivity(i)
                    overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left)
                }
                if(sharedSettingsData.getString("settingsLogin", "") == "InfoActivity"){
                    val i = Intent(this, InfoActivity::class.java)
                    startActivity(i)
                    overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left)
                }
            }
            catch (e: Exception){
                Log.d("[ACTIVITY EXCEPTION]", "Unable to find correct activity to intent to. Code: $e")
            }
  • Related