我们先写两个Activity:A和B,试着A打开B观察它们的生命周期:
你会发现是这样运行的:(A)onCreate->(A)onStart()->(A)onResume()->A正在运行中,此时A需要跳转的到B->(A)onPause()->(B)onCreate->(B)onStart()->(B)onResume()->B正在运行中->(A)onStop()->(A)onDestory()。是不是你发现了什么猫腻,找到了解决方法了。
OK,我直接说方法吧:
1、定义一个全局变量进行计数,我是在Application中声明了一个变量:
/** 用于判断APP进入后台使用 */
public static int mAppActivityCount = 0;
2、在BaseActivity中的onStart()、onStop()方法中这样写:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
@Override protected void onStart() { try { super.onStart(); XYApplication.mAppActivityCount ++; if (XYApplication.mAppActivityCount == 1) { //第一次启动 }else { //由APP的其他Activity启动 } } catch (Exception e) { e.printStackTrace(); } } @Override protected void onStop() { super.onStop(); XYApplication.mAppActivityCount --; if (XYApplication.mAppActivityCount == 0) { //切到后台 }else { //启动其他Activity } } |
3、A和B Activity继承BaseActivity
总结:看懂了没有,没有的话你可以亲自试试哦,验证一下对不。不行的话,可以留言或加QQ群:179730949。
还有更高大上的方法:
Android中怎么判断是Home键或非正常退出(进入后台)续
文章评论
…We were born in to this world with God’s will and one day we will also leave this world at his will. Therefore we should live our lives how Allah (God) wills us to, and he has revealed to us the Qura&8217;#n (our Intructions and guide) but it does not stop there; he is so generous he has also given us an example, the best of examples: The Prophet Muhammed (PBUH). We should live our lives being aware that Allah (God) is all knowledgable and knows what we do, even in secret. Ask4more info
Your story was really inartmfoive, thanks!
Good Day, glad that i saw on this in bing. Thanks!
yahoo brought me here. Thanks!