简体   繁体   English

Android-恢复已运行的活动

[英]Android - Resuming already running activity

I have an android application which has only one activity having only one button. 我有一个只有一个按钮只有一个活动的android应用程序。 When we press this button, another thread starts which does some particular work. 当我们按下该按钮时,另一个线程开始执行某些特定的工作。 If the button is again pressed, then the running thread stops. 如果再次按下该按钮,则运行线程停止。 Now the problem is, if i leave the thread running and go to the home screen and again press the launcher icon of the app, it starts a new activity. 现在的问题是,如果我让线程继续运行并转到主屏幕,然后再次按应用程序的启动器图标,它将启动一个新活动。 Now if i click on the button of the activity, another thread starts. 现在,如果我单击活动的按钮,则另一个线程开始。 Now I have two threads running at the same time, which i don't want. 现在,我有两个线程同时运行,这是我不想要的。

I want to reopen the previous running activity and then when I click on the button again, the already running should stop. 我想重新打开上一个正在运行的活动,然后当我再次单击该按钮时,已经运行的应该停止。 I tried using launchmode: singleTask, singleInstance but nothing seems to be working. 我尝试使用launchmode:singleTask,singleInstance,但似乎没有任何效果。

Please help how to handle this problem. 请帮助如何处理此问题。

Try reading the docs @ http://developer.android.com/guide/topics/manifest/activity-element.html 尝试阅读文档@ http://developer.android.com/guide/topics/manifest/activity-element.html

-- -

"singleTop" “ singleTop”

Conditionally If an instance of the activity already exists at the top of the target task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity. 有条件的如果活动的实例已经存在于目标任务的顶部,则系统通过调用其onNewIntent()方法将意图路由到该实例,而不是创建活动的新实例。

Alternatively 或者

you can control the thread based on the onPause()/onCreate()/onResme(), pause in the onPause() create/start in the onCreate() restart the thread in the onResume() 您可以基于onPause()/ onCreate()/ onResme()控制线程,在onPause()中暂停创建/在onCreate()中启动在onResume()中重新启动线程

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

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