简体   繁体   English

Android应用:管理活动

[英]Android app: managing activities

I am trying to learn android programming. 我正在尝试学习android编程。 I have made a basic app. 我做了一个基本的应用程序。 It has a main activity, a settings activity and a notification that opens the settings activity on click. 它具有一个主要活动,一个设置活动和一个在单击时打开设置活动的通知。 Main activity also has a settings button that opens settings activity. 主活动还具有一个设置按钮,用于打开设置活动。 It may sound weird but i am trying to learn only. 听起来可能很奇怪,但我只想学习。 So the problem is when i open the settings activity and then click notification, it again opens settings activity on each click on notification. 所以问题是当我打开设置活动然后单击通知时,它再次在每次单击通知时打开设置活动。 Then when i hit back button on my phone, it takes me again to settings activity and i have to press back button as many times as i clicked the notification to go back to main activity. 然后,当我在手机上单击“后退”按钮时,它将再次带我进行设置活动,并且我必须按“后退”按钮的次数与单击通知以返回至主要活动的次数相同。 Looks like the settings activity is being instantiated again and again on each click on notification. 似乎每次单击通知时一次又一次实例化设置活动。 I have tried using many things i read online like trying some Intent flags but can't find anything working. 我尝试使用许多在线阅读的内容,例如尝试一些Intent标志,但找不到任何有效的方法。 I tried using finish(); 我尝试使用finish(); in onBackPressed in my settings activity but doing this takes me to main activity and when i hit back on main activity, it takes me to settings and then again i have to hit back that number of times. 在我的设置活动中的onBackPressed中,但是执行此操作会将我带到主要活动,而当我回击主要活动时,它将带我进入设置,然后再次必须回击该次数。

How do i make the notification open the settings activity only if its not running? 如果通知未运行,如何使通知打开设置活动?

I think this can help you. 我认为这可以为您提供帮助。

Edit in AndroidManifest.xml 在AndroidManifest.xml中编辑

<activity android:name=".SettingActivity" android:launchMode="singleTask">

This will open the activity if its not running. 如果活动未运行,将打开该活动。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM