1. Are you ready for the Galaxy S20? Here is everything we know so far!

Context Menu Screen Rotation

Discussion in 'Android Development' started by timcs, Dec 21, 2013.

  1. timcs

    timcs Member
    Thread Starter

    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
     



    1. Download the Forums for Android™ app!


      Download

       
  2. timcs

    timcs Member
    Thread Starter

    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
     
  3. timcs

    timcs Member
    Thread Starter

    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
     
  4. alostpacket

    alostpacket Over Macho Grande?

    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.
     
  5. timcs

    timcs Member
    Thread Starter

    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
     
  6. alostpacket

    alostpacket Over Macho Grande?

  7. timcs

    timcs Member
    Thread Starter

    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
     
  8. alostpacket

    alostpacket Over Macho Grande?

    Oh, I see.

    Yeah ContextMenu is odd. There is a way to programatically open it, but if you find an AlertDialog works for you, that's just as easy.
     
Loading...

Share This Page

Loading...