如何使用 Kotlin 和 Conductor 处理 Intent
How to handle Intent with Kotlin and Conductor
我有三个视图和一个 activity。
MainActivity - MainController - PushedController - NotificationOpenController.
MainController 包含一个按钮,按下时会执行一些黑框操作,并且设备会在通知托盘中收到通知。
点击通知时,我希望行为如同
router.pushController(RouterTransaction.with(NotificationOpenController(bundle))
.pushChangeHandler(VerticalChangeHandler())
.popChangeHandler(VerticalChangeHandler()))
刚刚发生。
感谢任何与 Kotlin 相关的提示或修订,因为我这样做是为了学习这门语言
我不确定如何通过 Intents 以正确的方式实现这一点,也许是广播?但经过反复试验,我的代码现在看起来像这样:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
App.graph.inject(this)
setContentView(R.layout.activity_main_kotlin)
Log.d(TAG,"${FirebaseInstanceId.getInstance().getToken()}")
router = Conductor.attachRouter(this, controller_container, savedInstanceState)
val startingIntent : Intent? = intent
if(startingIntent?.extras != null){
transitionToNotificationOpenController(startingIntent?.extras)
}
else{
if (!router.hasRootController()) {
router.setRoot(RouterTransaction.with(MainController()));
}
}
}
override fun onBackPressed() {
if (!router.handleBack()) {
super.onBackPressed()
}
}
fun transitionToNotificationOpenController(bundl: Bundle?){
router.pushController(RouterTransaction.with(NotificationOpenController(bundl))
.pushChangeHandler(VerticalChangeHandler())
.popChangeHandler(VerticalChangeHandler()))
}
通知意图是这样构建的
val builder = NotificationCompat.Builder(this).setContentTitle("Title").setContentText("Text")
val notificationIntent = Intent(this, MainActivity::class.java)
notificationIntent.putExtra("message",messageBody)
val contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT)
...
manager.notify(1,builder.build())
但是,使用这种方法动画会出错,用户只能看到 "typical" - 在加载一些内容或启动新内容后弹出视图 activity-
因为按下通知时有 Activity Resume,按下后退按钮后 Activity Pause -> Activity Resume。
达到预期结果。
<activity android:name=".ui.MainActivity"
android:launchMode="singleTop"
>
主要活动:
override fun onResume(){
super.onResume()
val bundle : Bundle? = intent.extras
if (bundle != null){
transitionToNotificationOpenController(bundle)
}
}
/**
* MainActivity is defined in AndroidManifest.xml as android:launchMode="singleTop"
* onNewIntent acts as an entrypoint whenever an intent is received pointing to MainActivity,
* since we don't want to launch a new instance of the Activity.
* (because we want to keep the stack of RouterTransactions)
*/
override fun onNewIntent(intent : Intent){
setIntent(intent)
}
我有三个视图和一个 activity。 MainActivity - MainController - PushedController - NotificationOpenController.
MainController 包含一个按钮,按下时会执行一些黑框操作,并且设备会在通知托盘中收到通知。
点击通知时,我希望行为如同
router.pushController(RouterTransaction.with(NotificationOpenController(bundle))
.pushChangeHandler(VerticalChangeHandler())
.popChangeHandler(VerticalChangeHandler()))
刚刚发生。
感谢任何与 Kotlin 相关的提示或修订,因为我这样做是为了学习这门语言
我不确定如何通过 Intents 以正确的方式实现这一点,也许是广播?但经过反复试验,我的代码现在看起来像这样:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
App.graph.inject(this)
setContentView(R.layout.activity_main_kotlin)
Log.d(TAG,"${FirebaseInstanceId.getInstance().getToken()}")
router = Conductor.attachRouter(this, controller_container, savedInstanceState)
val startingIntent : Intent? = intent
if(startingIntent?.extras != null){
transitionToNotificationOpenController(startingIntent?.extras)
}
else{
if (!router.hasRootController()) {
router.setRoot(RouterTransaction.with(MainController()));
}
}
}
override fun onBackPressed() {
if (!router.handleBack()) {
super.onBackPressed()
}
}
fun transitionToNotificationOpenController(bundl: Bundle?){
router.pushController(RouterTransaction.with(NotificationOpenController(bundl))
.pushChangeHandler(VerticalChangeHandler())
.popChangeHandler(VerticalChangeHandler()))
}
通知意图是这样构建的
val builder = NotificationCompat.Builder(this).setContentTitle("Title").setContentText("Text")
val notificationIntent = Intent(this, MainActivity::class.java)
notificationIntent.putExtra("message",messageBody)
val contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT)
...
manager.notify(1,builder.build())
但是,使用这种方法动画会出错,用户只能看到 "typical" - 在加载一些内容或启动新内容后弹出视图 activity- 因为按下通知时有 Activity Resume,按下后退按钮后 Activity Pause -> Activity Resume。
达到预期结果。
<activity android:name=".ui.MainActivity"
android:launchMode="singleTop"
>
主要活动:
override fun onResume(){
super.onResume()
val bundle : Bundle? = intent.extras
if (bundle != null){
transitionToNotificationOpenController(bundle)
}
}
/**
* MainActivity is defined in AndroidManifest.xml as android:launchMode="singleTop"
* onNewIntent acts as an entrypoint whenever an intent is received pointing to MainActivity,
* since we don't want to launch a new instance of the Activity.
* (because we want to keep the stack of RouterTransactions)
*/
override fun onNewIntent(intent : Intent){
setIntent(intent)
}