سؤال

I wrote a class that extends popupwindow. When the user presses the back key, I would like to handle the back pressed from this class as opposed to having the underlying activity handle it (i.e., I already know about overriding onBackPressed(), but this requires modifying the activity's code). I have a reference of the activity instance. Is there anyway to override the on back pressed key when the popupwindow is displayed without using the onBackPressed() method from within the activity's code?

هل كانت مفيدة؟

المحلول

There is no way unless you hijack the back button from the OS and have it broadcast to wake up your app's PopupWindow (I do not suggest doing this).

You should override onBackPressed() or I believe onKeyDown(int keyCode, KeyEvent event) in your Activity to act on/consume the back key (while your activity is in the foreground). As far as having it execute something "dynamic", I'm not exactly sure I understand what you are asking. You can have the code in your PopupWindow class, but call it from onBackPressed() or onKeyDown() through your instance variable in your activity.

نصائح أخرى

if I am not wrong what you are trying to do actually is invoke the onBackpressed() method of the activity from inside another class.I guess you can define a constructor of your class that takes in a context parameter and using this context you can invoke the onBackPressed() method of your activity. I hope this helps..

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top