Skip to content

Commit

Permalink
update: add intent feature
Browse files Browse the repository at this point in the history
  • Loading branch information
amirisback committed Nov 16, 2024
1 parent 0da4b6e commit 0ae44e5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.qomunal.opensource.androidresearch.common.ext

import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity

/**
* Created by faisalamircs on 13/01/2024
* -----------------------------------------
Expand All @@ -10,3 +14,21 @@ package com.qomunal.opensource.androidresearch.common.ext
*/


inline fun <reified T> AppCompatActivity.getIntentExtraExt(params: String): T? {
return intent.getIntentExtraExt(params)
}

inline fun <reified T> Intent.getIntentExtraExt(params: String): T? {
if (this.hasExtra(params)) {
val data = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
this.getParcelableExtra(
params,
T::class.java
)
} else {
@Suppress("DEPRECATION") this.getParcelableExtra(params)
}
return data
}
return null
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Bundle
import androidx.activity.viewModels
import com.qomunal.opensource.androidresearch.common.base.BaseActivity
import com.qomunal.opensource.androidresearch.databinding.ActivityDetailBinding
import com.qomunal.opensource.androidresearch.common.ext.getIntentExtraExt

/**
* Created by faisalamircs on 13/01/2024
Expand All @@ -17,11 +18,21 @@ import com.qomunal.opensource.androidresearch.databinding.ActivityDetailBinding

class DetailActivity : BaseActivity<ActivityDetailBinding>() {

companion object {
val TAG = DetailActivity::class.java.name
const val TEXT_KEY_EXTRA = "TEXT_KEY_EXTRA"
}

private val viewModel: DetailViewModel by viewModels()

private val reouter: DetailRouter by lazy {
DetailRouter(this)
}

private val extra : String? by lazy {
getIntentExtraExt(TEXT_KEY_EXTRA)
}

override fun setupViewBinding(): ActivityDetailBinding {
return ActivityDetailBinding.inflate(layoutInflater)
}
Expand All @@ -32,7 +43,7 @@ class DetailActivity : BaseActivity<ActivityDetailBinding>() {

override fun initUI() {
binding.apply {

tvText.text = extra
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.qomunal.opensource.androidresearch.ui.main

import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import com.qomunal.opensource.androidresearch.common.base.BaseActivity
import com.qomunal.opensource.androidresearch.databinding.ActivityMainBinding
import com.qomunal.opensource.androidresearch.ui.detail.DetailActivity

class MainActivity : BaseActivity<ActivityMainBinding>() {

Expand All @@ -24,6 +26,9 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
binding.apply {
btnTest.setOnClickListener {
// TODO your work
startActivity(Intent(this@MainActivity, DetailActivity::class.java).apply {
putExtra(DetailActivity.TEXT_KEY_EXTRA, "Test")
})
}
}
}
Expand Down

0 comments on commit 0ae44e5

Please sign in to comment.