How to get any identifier of the topmost activity?

前端 未结 2 1238
春和景丽
春和景丽 2020-11-30 07:09

I have a service and its behavior must change when topmost Activity changes. Say, Activity A is active and then service starts some kind of processing. This processing must

2条回答
  •  庸人自扰
    2020-11-30 07:47

    Create a baseActivity and extends all other Activities from baseActivity, in onResume save Current Activity in a public static Filed in a Global class (in my case extends Application)

    public class baseActivity extends Activity {
        @Override
        protected void onResume() {
            super.onResume();
            Global.Activity = this;
        }
    }
    

    Other Activities

        public class mainActivity extends baseActivity {
            // You Can Use Global.Activity Everywhere ! , Even in a Object Extended Class
        }
    

提交回复
热议问题