Edward van Raak
Edward van Raak

Reputation: 4910

Android: setRetainInstance(true) removes navigation mode spinner in ActionBar on orientation change

I have a SherlockListFragment that puts a navigation mode list spinner inside the actionbar by using setNavigationMode(ActionBar.NAVIGATION_MODE_LIST). This works fine but I noticed that the navigation spinner disappears when changing orientation. I managed to figure out it's because I call setRetainInstance(true);, setting this to false fixes the problem of the disappearing navigation spinner but I really need to retain the instance of this fragment so setting it to false is no option.

I tried recalling setNavigationMode(ActionBar.NAVIGATION_MODE_LIST) on orientation change but this didn't fix the problem. The spinner is just gone forever it seems...

What causes the navigation spinner inside the Actionbar to disappear when changing orientation? How do I bring it back?

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);         
        AsyncTask<String, Integer, String[]> asynctask = new DownloadFilesTask(this).execute(url);
        setRetainInstance(true);        
        Context context = getSherlockActivity().getSupportActionBar().getThemedContext();
        ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.options, R.layout.sherlock_spinner_item);
        list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
        getSherlockActivity().getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);        
        getSherlockActivity().getSupportActionBar().setListNavigationCallbacks(list, this);
    }

Edit:

Somebody posted a solution to use android:configChanges="orientation" and deleted his answer. I looked into that and android:configChanges="orientation|screenSize" did fix the problem for me. However, this seems more like a workaround than an actual solution. O well...

Upvotes: 0

Views: 494

Answers (1)

dmon
dmon

Reputation: 30168

So, why are you trying to set the Activity's navigation based on a list in the Fragment? It seems like something the Activity should be doing. If you really need the fragment to do this, the fragment can call a method on the owning activity (if it implements an interface) to set the options. Just set the list of options, not the actual adapter (the activity should own the adapter). The activity can, in turn, call a method on the fragment to indicate an option was selected.

Upvotes: 1

Related Questions