简体   繁体   English

Android:最小化和恢复活动

[英]Android: Minimizing and resuming a Activity

I am pretty new to Android development . 我是Android开发的新手。

My problem is a bit tricky one. 我的问题有点棘手。

I want to develop an application using 2 activities. 我想使用2个活动来开发应用程序。

1st activity has a button . 第一个活动有一个按钮。 On clicking the button I want activity 2 to get started as follows: 单击按钮后,我要开始活动2,如下所示:

Activity 2 will come to foreground for 2 seconds and then goes to background for 8 seconds , after which it will again come to foreground for 2 seconds and then again goes to background and the process continues. 活动2将进入前台2秒钟,然后进入后台8秒钟,此后它将再次进入前台2秒钟,然后再次进入后台,然后继续该过程。

Meanwhile both activities should continue their respective tasks. 同时,两项活动应继续各自的任务。

For ex. 对于前。 We can have a Activity such as Music player which is playing music and another activity named Activity 2 which is downloading some files. 我们可以有一个活动,例如正在播放音乐的音乐播放器,以及另一个名为活动2的活动,该活动正在下载一些文件。

I have tried many things ranging from using Intents to minimizing a activity and displaying notification on notification bar. 我尝试了很多事情,从使用Intent到最小化活动以及在通知栏上显示通知。 The problem with notification bar is that it on resuming the activity using notification bar it is always calling OnCreate() method thus again starting the activity. 通知栏的问题在于,它使用通知栏恢复活动时始终调用OnCreate()方法,从而再次启动活动。

I am able to start a activity on button click but don't know how to minimize it and then pop it up in same state . 我可以通过单击按钮来启动活动,但是不知道如何最小化该活动,然后将其弹出来处于相同状态。

I am using services in background for timing delays . 我在后台使用服务来延迟时间。

Please Help and share your solutions to this problem. 请帮助并分享您对这个问题的解决方案。

I have solved the problem. 我已经解决了问题。

The solution is to modify manifest file as : 解决方案是将清单文件修改为:

add to main activity code 添加到主要活动代码

android:launchMode="singleTask"

android:clearTaskOnLaunch="true"

add to child activity code 添加到子活动代码

android:launchMode="singleInstance"

android:clearTaskOnLaunch="true"

Its works perfectly for my app :) 它非常适合我的应用程序:)

thank you all for your kind contribution 谢谢大家的贡献

Maybe u found solution for this issue as u said, but in any case, I would recommend reading some things.. It will help you to better understand that: 也许您像您所说的那样找到了解决此问题的方法,但是无论如何,我建议您阅读一些内容。它将帮助您更好地理解:

Oki.. I made some effort and organize links for you. Oki ..我做了些努力,并为您组织链接。 I know it seems a lot of material, but you don't have to run to read them in next 15min.. Take it easy, read, try, learn. 我知道这似乎有很多内容,但是您不必在接下来的15分钟内阅读它们。轻松一点,阅读,尝试,学习。 Take this as advice , cause once u get used to do things in a wrong way it will be much more difficult to correct them later.. 以此为指导 ,因为一旦您习惯以错误的方式做事,以后要纠正它们将变得更加困难。

Hope you find it useful.. ;) Cheers 希望你觉得有用。

moveTaskToBack(true); moveTaskToBack(真); moves the whole task to back ie it moves both activities to back . 将整个任务移至后退,即将两项活动移至后移。 Then the problem will be to move the activity to foreground which I don't know. 然后的问题将是将活动移到我不知道的前景。

Have you tried using 您是否尝试过使用

this.moveTaskToBack(true);

I'm not sure if this is what your looking for or not. 我不确定这是否是您要寻找的。 Heres the Documentation as well! 这也是文档

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

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