【背景】
android项目中,需要根据当前处于哪个activity去决定后续动作。
所以需要去获得当前处于哪个activity。
【解决过程】
1.搜:
android check current activity
参考:
How to get current foreground activity context in android? – Stack Overflow
感觉很麻烦啊。
2.参考:
android – How to check if my activity is the current activity running in the screen – Stack Overflow
需要每个activity都去给onResume添加代码,也很麻烦。
3.搜:
android current which activity
参考:
没找到合适的。
4.还是参考:
How to get current foreground activity context in android? – Stack Overflow
去给每个onResume,onDestroy,onPause,都加上对应的设置。
【总结】
最后是通过上述办法,给每个Activity都添加对应的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @Override protected void onResume() { super .onResume(); AppContext.getInstance().setCurrentActivity( this ); } @Override protected void onPause() { AppContext.getInstance().setCurrentActivity( null ); super .onPause(); } @Override public void onDestroy() { AppContext.getInstance().setCurrentActivity( null ); super .onDestroy(); } |
然后在自己的app中:
此处我的是:
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | import android.app.Activity; import android.app.Application; public class AppContext extends Application{ private Application parentApp; private Activity curActivity; public Application getAppContext() { return parentApp; } private static class LazyHolder { static AppContext instance = new AppContext(); } public static AppContext getInstance() { return LazyHolder.instance; } @Override public boolean Start(Map<String, Object> args) { Object param = args.get(appContext); if (param instanceof Application) { parentApp = (Application)param; return true ; } return false ; } @Override public void Stop(Map<String, Object> args) { } public void setCurrentActivity(Activity curActivity){ this .curActivity = curActivity; } public Activity getCurrentActivity(){ return this .curActivity; } public boolean isInAdapterActivity(){ boolean isInAdapterActivity = false ; if ( null != curActivity){ if (curActivity instanceof AdapterActivity){ isInAdapterActivity = true ; } } return isInAdapterActivity; } public boolean isInScanActivity(){ boolean isInScanActivity = false ; if ( null != curActivity){ if (curActivity instanceof ScanActivity){ isInScanActivity = true ; } } return isInScanActivity; } public boolean isInMainActivity(){ boolean isInMainActivity = false ; if ( null != curActivity){ if (curActivity instanceof MainActivity){ isInMainActivity = true ; } } return isInMainActivity; } } |
然后在对应各个Activity:
- AdapterActivity
- ScanActivity
- MainActivity
中就可以去调用:
1 2 3 | if (AppContext.getInstance().isInMainActivity()){ //do sth. } |
了。
【后记】
1.后来是再次去看了:
的Activity的生命周期图:
更加明确了上面方案的含义:
对应任何的Activity来说,只要处于:
当前运行(Running)
的状态,则都必须经过
onResume
所以,在onResume中,设此时Activity为当前的Activity。
对应的:
当Activity从Running变成别的状态时,也会经过onPause,所以在onPause中去设置当前Activity为null
(切换到别的Activity时,就进入了别的Activity的onResume了,就把别的Activity设置为当前的Activity了)
以及:
当Activity处于onDestory后,也同样设置当前Activity为null)
此时继续之前的循环:
如果进入别的Activity的话,自然会进入onResume,自然会设置别的Activity为当前正在运行的Activity。
2.另外抽空也可以看看这个:
Pausing and Resuming an Activity | Android Developers
转载请注明:在路上 » 【已解决】Android中判断当前处于哪个Activity