Last active
June 10, 2021 17:19
-
-
Save hamurcuabi/1c67855da38e438733244ac4d2f93ea6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plugins { | |
id 'com.android.application' | |
id 'kotlin-android' | |
id 'kotlin-android-extensions' // unutma | |
} | |
implementation 'androidx.activity:activity-ktx:1.3.0-alpha07' | |
implementation 'androidx.fragment:fragment-ktx:1.3.3' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
lateinit var binding: ActivityMainBinding | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding = ActivityMainBinding.inflate(layoutInflater) | |
setContentView(binding.root) | |
binding.btnGo.setOnClickListener { | |
customContract.launch(User("Hamurcu", 26)) | |
} | |
binding.btnPickImage.setOnClickListener { | |
pickImage.launch("image/*") | |
} | |
binding.btnMultiPerm.setOnClickListener { | |
askMultiplePermissions.launch(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CAMERA)) | |
} | |
} | |
private val customContract = registerForActivityResult(SimpleContract()) { data -> | |
binding.textView.text = data | |
} | |
private val requestPermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted -> | |
binding.textView.text = granted.toString() | |
} | |
private val askMultiplePermissions = | |
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { map -> | |
for (entry in map.entries) { | |
Toast.makeText(this, "${entry.key} = ${entry.value}", Toast.LENGTH_SHORT).show() | |
} | |
} | |
private val pickImage = | |
registerForActivityResult(ActivityResultContracts.GetContent()) { uri -> | |
binding.textView.text = uri?.toString() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Intent | |
import android.os.Bundle | |
import androidx.appcompat.app.AppCompatActivity | |
import com.emrehmrc.activitycontract.databinding.ActivitySecondBinding | |
class SecondActivity : AppCompatActivity() { | |
lateinit var binding: ActivitySecondBinding | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding = ActivitySecondBinding.inflate(layoutInflater) | |
setContentView(binding.root) | |
val user = intent.getParcelableExtra<User>(KEY_USER_OBJECT) | |
binding.apply { | |
txtUser.text = user.toString() | |
btnSetResult.setOnClickListener { setResult() } | |
} | |
} | |
private fun setResult() { | |
val intent = Intent() | |
intent.putExtra(KEY_RESULT, "Başarılııı!") | |
setResult(RESULT_OK, intent) | |
finish() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity | |
import android.content.Context | |
import android.content.Intent | |
import android.os.Parcelable | |
import androidx.activity.result.contract.ActivityResultContract | |
import kotlinx.android.parcel.Parcelize | |
//Create Custom Contract | |
const val KEY_USER_OBJECT = "KEY_USER_OBJECT" | |
const val KEY_RESULT = "RESULT" | |
@Parcelize | |
data class User(val name: String, val age: Int) : Parcelable | |
class SimpleContract : ActivityResultContract<User, String?>() { | |
override fun createIntent(context: Context, input: User): Intent { | |
val intent = Intent(context, SecondActivity::class.java) | |
intent.putExtra(KEY_USER_OBJECT, input) | |
return intent | |
} | |
override fun parseResult(resultCode: Int, intent: Intent?): String? = when { | |
resultCode != Activity.RESULT_OK -> null | |
else -> intent?.getStringExtra(KEY_RESULT) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment