Without activitieventlistener, how can I implement the following code?