简体   繁体   English

从通知意图启动活动时重新创建 Android ViewModel

[英]Android ViewModel recreated when launching activity from notification intent

I'm trying to create a timer app.我正在尝试创建一个计时器应用程序。 When a user presses the timer, it will create a notification that the timer is running.当用户按下计时器时,它将创建一个计时器正在运行的通知。

I'm using a view model to store my view data.我正在使用视图 model 来存储我的视图数据。 What I would like is when the timer is started and the notification is created, when the user presses the notification, it launches the activity for the timer again and brings it to the top of the stack.我想要的是当计时器启动并创建通知时,当用户按下通知时,它会再次启动计时器的活动并将其带到堆栈的顶部。 The activity's view should retain the same view model data as before.活动的视图应保留与以前相同的视图 model 数据。

What's currently happening is that clicking on the notification creates a new activity in my back stack.当前发生的是单击通知会在我的后台堆栈中创建一个新活动。 This creates a new view model for this activity.这将为此活动创建一个新视图 model。 When I press the back button, I can return to the old activity view.当我按下后退按钮时,我可以返回到旧的活动视图。

I just need this old activity view to be brought back up to the stack, not to create a new activity.我只需要将这个旧的活动视图带回堆栈,而不是创建新的活动。

My code in the view model to send a notification when the timer starts is this:我在视图 model 中的代码在计时器启动时发送通知是这样的:

    private fun sendNotification() {

    val intent = Intent(context, MainActivity::class.java)

    val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)

    var builder = NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_launcher_foreground)
        .setContentTitle("Pomodoro Timer")
        .setContentText("Pomodoro Timer is running")
        .setContentIntent(pendingIntent)
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)

    with(NotificationManagerCompat.from(context)) {
        // notificationId is a unique int for each notification that you must define
        notify(1, builder.build())
    }
}

My view model from my activity onCreate is created like this:我的活动 onCreate 中的 model 视图是这样创建的:

    val dataBinding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)

    val timerFactory = TimerFactory(this)
    val timer = ViewModelProvider(this, timerFactory).get(Timer::class.java)

    dataBinding.timer = timer
    dataBinding.lifecycleOwner = this

How do I make my intent on the notification to retain and just bring the old activity and view model back to the top of the stack?如何使我的通知意图保留并仅将旧活动和查看 model 回到堆栈顶部?

The pending intent in the notification is starting a new task, hence a new instance of MainActivity is created.通知中的待处理意图正在启动一个新任务,因此会创建一个新的 MainActivity 实例。

If you want to get back to the original MainActivity, declare MainActivity in Manifest as android:launchMode="singleTask" .如果你想回到原来的 MainActivity,在 Manifest 中声明 MainActivity 为android:launchMode="singleTask"

Also check the docs about Android launch modes.另请查看有关 Android 启动模式的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android - 从通知启动活动 - Android - Launching Activity from Notification Android通知意图未启动所述活动和通知声音问题 - Android Notification Intent Not launching the said Activity and Notification Sound Issue 显示新活动时重新创建活动/视图模型 - Activity/ViewModel recreated when new Activity is shown Android意向未启动活动 - Android intent not launching activity Android活动意图启动 - Android activity intent launching 从通知启动意图时,“从非活动上下文服务调用的startactivity”警告 - 'startactivity called from non-activity context service' warning when launching intent from notification 重新创建活动时使用原始意图 - Using the original Intent when activity is recreated Android ViewModel在其主机活动不在活动堆栈顶部并且设备已旋转时重新创建 - Android ViewModel recreated when its Host Activity was not in the top of Activity Stack and the device was rotated 当活动从后台(暂停)到前台时,将重新创建Android Activity - Android Activity is recreated when activity from background(pause) to foreground Android在按下通知时如何进行活动 - Android how to intent activity when notification is pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM