• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Context Menu Screen Rotation

timcs

Member
Sep 18, 2013
61
6
I have been trying to keep the context menu when doing a long press / Click to stay on screen when the screen is rotated.

Searching around the net, the main answers found are not the best such as add configChanges into the android manifest or use the onRetainNonConfigurationInstance which is depreciated .

Currently I am handling my dialog boxes when on screen and a rotation occurs via savedInstanceState by having my own method to show them again by using a variable to be saved into the bundle and then retrieved.

However the context menu is slightly harder to do as it is registered against the listview I have and this links it together so it knows which row has been selected

I do also have a onitemlongclick method which works along side the contextmenu so I should be able to get the row information from there but it is then how to call it all back afterwards

Thanks

TimCS
 
further to this I have been trying to use the showcontextmenu() & showcontextmenuforchild(view) however because I am trying to force call getOnItemLongClickListener() I cannot get it to call this method using this way. As from within here I am then trying to call the above methods showcontextmenu and showcontextmenuforchild(view) from within the onItemLongClickListener by checking a variable value.

At the moment I can make the highlighted row stay highlighted but that is only because my custom simplecursoradapter is looking for the variable that I have stored in the saveInstancestate when the device is rotated.

Thanks

TimCS
 
Upvote 0
I have noticed that when I rotate the device , logcat reports a memory leak when this context menu is on screen and refers to an intent receiver needing to be unregistered even though in this activity there is no intent receiver actually registered.

I presume it is referring to unregistering the context menu but I have tried doing this via the onPause method and then leaving the register in the onCreate to link it back to the listview.

Now, is this the reason why I cannot get the context menu to reload when the device has been rotated?

Thanks

TimCS
 
Upvote 0
can you keep a reference to the context menu then call dismiss on it?

I'm a bit rusty on how Activities handle this (it's been awhile).

The preferred way these days is via DialogFragments using the support library which get saved with the fragment instance automatically.


Though I would only switch to Fragments when you are comfortable -- it's a big change.
 
Upvote 0
can you keep a reference to the context menu then call dismiss on it?

I'm a bit rusty on how Activities handle this (it's been awhile).

The preferred way these days is via DialogFragments using the support library which get saved with the fragment instance automatically.


Though I would only switch to Fragments when you are comfortable -- it's a big change.

Hi Alostpacket thanks for your reply , sadly I cannot use fragements as my API starts at level 8 up to 17 so I need to find a way of handling it so that it works across all of these SDK levels.

As for your suggestion on keeping a reference to it I am not sure how or may be I have misunderstood you (more than likely). I am storing a variable to mark when it has been activated but I am struggling to then re-activate after the device has been rotated.

I have used the onResume , onCreate, onLongItemClick a method I call at the end of onCreate which builds up the data and calls the custom adapter but I am finding it will not display the context menu.


*** EDIT 17:08 ****
Might considering not using the context menu and instead use a single choice alert dialog box at least with this I can handle it better when the screen has rotated.


Thanks

TimCS
 
Upvote 0
Upvote 0
You basically need to dismiss the dialog (maybe in onStop() ) then re-create it based on whatever data you saved in the bundle ( onSaveInstanceState() ).


Something like this:
android - onSaveInstanceState () and onRestoreInstanceState () - Stack Overflow

I wouldnt use onRetainNonConfigurationInstance() unless you need to.


As for Fragments, when you are ready, you would use the support library version of Fragments.

(FYI most people use v4)
Support Library | Android Developers

.

Hi again alostpacket, the context menu is not quite the same as a standard dialog and so to dismiss it means to closemenucontext which I have tried in onSaveInstance and I am using this already to retain the variable to state that this context menu has been opened.

The issue is not how to hold the variable to state whether the menu was opened at the time the device was rotated but how to actually bring the context menu back.

However I have decided to try and use the alert dialog box with a single choice but thanks for you help on this alostpacket.

I have now moved onto another part of my app for now and having fun with that SQLite , Date fields and date ranges. However I am not currently holding my dates in a date field but just integer values and I am getting very strange results from trying to bring up a range of records via a date range.

Thanks

TimCS
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones