Android : Android - Better to keep ActionBar in variable or call getActionBar

on Friday, December 12, 2014


I was starting to code in Android, but one doubt came to my mind. I need to access the ActionBar's of the Activities inside Fragments, but each time the Android is rotated, the ActionBar is recreated inside the OnCreate of the Activity. So, I do need to get each time the new ActionBar inside the OnCreateView of the Fragment. But the question is, what has better performance: save the ActionBar object inside OnCreateView in a Fragment's private variable like:



bar = new SetActionBar((ActionBarActivity)getActivity()).getSupportActionBar();


Or should I just always use:



((ActionBarActivity)getActivity()).getSupportActionBar().ANY_DESIRED_METHOD_TO_BE_EXECUTED_BY_ACTIONBAR();


The SetActionBar is a custom class that I created to correctly avoid NullPointer exceptions and to do some custom implementations of the methods already present inside the ActionBar class and also to facilitate porting the Theme.AppCompat actionbar to any other type of actionbar (like ActionBarSherlock).


0 comments:

Post a Comment