Scenario :
An activity is displayed. It has a text box and a button. If you enter a special code in the textbox and click the button the activity closes after comparing the text logic. Simple! This has been implemented in the OnCLick() of the the activity.
Problem :
I have a library that handles all the the SMS receiver functionalities. The receiver remotely listens for a special incoming SMS. Once the special sms arrives, a library function (closeActivity) should either
-
finish() the activity(described above) straightaway
-
imitate the action of entering text + clicking button by getting a reference to the activity somehow?
I am fine with either. which ever is simpler and safer!
I just need to know how/where/what code to write in the library so that it can finish() the activity!
Assumption: The activity is always displayed and the system is not stacking it automatically!
Why dont you try to use intents? esp broadcast intents.. Fire the broadcast intent from library and get it in ur activity /another class registered as intent reciever . You can get the instance of activity in the broadcast reciever easily.
public static <Your activity class> activitySingleTonand set the singleton in appropriate lifecycle method.
OnCreate(){ activitySingleTon = this; }and use in broadcast reciever like this
if(<your activity class>.activitySingleTon){ <your activity class>.activitySingleTon.finish(); }