如何处理与Kotlin和指挥的意图

我有三个意见和一个活动。 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()) 

但是,使用这种方法,动画会全部错误,用户只能在加载一些或启动新的活动后才能看到“典型” – 弹出视图,因为按下通知时有一个活动恢复,随后是活动暂停 – >活动按下后退按钮后继续。

达到了预期的效果。

 <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) }