Office Hours — Today, November 2

Yesterday, November 1

Nov 2
8:55 AM
Mark M.
has entered the room
Mark M.
turned on guest access
9:10 AM
Mathias
has entered the room
Mathias
Heyhey
Mark M.
whoops -- hello, Mathias!
how can I help you today?
Mathias
Did you spill your coffee? :)
9:15 AM
Mark M.
no, but I got distracted and hadn't noticed that you entered the chat
sorry for any delay
Mathias
oh i came in 5 secs before literally
Hope all is well! Anyways, i have a quick question about PreferenceScreen
Mark M.
go right ahead
Mathias
View paste
I am working on re-making my settings screen from a custom one (that looks pretty bad) into using the PreferenceScreen. I need to use a fragment, since i have the settings in one of my tabs.

You discuss this topic in the book, but i have only found topics related to moving between activities for sub-categories. However, i have made another homegrown solution with a “back” button and manually switching between the PreferenceScreens, since a lot of functionality in the “regular” Preferences library is missing in compat. 

It seems to work fine, but i am a bit anxious about having missed something, for example related to memory leaks.
I have made an SO thread about this, so i was wondering if you’d have time to skim through it and see if you can see any issues with my code, or if there is something i’m not thinking about?
Mark M.
um, if you're willing to post the code somewhere that can be linked to from the chat, I can do a quick code review
Mathias
i can paste here if you want, it's not much
Mark M.
OK
Mathias
View paste
but the basic gist is:

1. Store away the "root" PreferenceScreen in onCreatePreferences so that i can go back later.

2. When the user clicks on the sub-category, the framework will navigate to the subscreen, and onPreferenceStartScreen will be called. Set the back button to visible there.

3. When back button is clicked, call the "onPreferenceStartScreen" manually with the root PreferenceScreen, and hide the back button again.

This seems to work well for me. Obviously there's no back stack, but i can live with that, this fragment lives in a tabbed application.
View paste (40 more lines)
public class SettingsFragment extends PreferenceFragmentCompat implements PreferenceFragmentCompat.OnPreferenceStartScreenCallback {

private Button back;
PreferenceScreen root;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    /**doing other stuff here**/
    setupBackButton();
}

private void setupBackButton(){
    back = getView().findViewById(R.id.leftButton);
    back.setText(R.string.action_back);
...
9:20 AM
Mark M.
¯\_(ツ)_/¯
Mathias
so basically i want to check with the expert (that's you!) if you can see any problem with storing away the preference in a class variable like this, or if you can see any other issue
heh, ok
Mark M.
nothing leaps out at me, other than perhaps 1-2 superfluous methods (e.g., onViewCreated())
I don't know what a PreferenceScreen all holds, but technically you're not leaking anything, so long as this fragment eventually goes away
Mathias
you're right i will remove. I was experimenting with handling the back stack.
allright, cool. That's all i had!
Mark M.
this is roughly equivalent to inflating a layout in onCreateView(), stashing it somewhere, and inflating a second layout in onCreateView(), returning that one, and later using the first layout
so long as your UI is doing what you want (i.e., hiding and showing the relevant widgets), there's nothing intrinsically wrong with that
Mathias
yeah, i do that in a mapfragment, moving back and forth with viwe overlays
Mark M.
I'm not completely sold on the benefit of this approach versus just having separate fragments and ordinary back stack behavior
but, if you like what you have, and things like LeakCanary aren't yelping about, roll with it!
9:25 AM
Mathias
Well, the benefit would be that the OS-level back would work. Since i don't have back stack when the user clicks the different tabs either, i thought it'd be confusing. But perhaps i'll revisit later. As a sidenote, the os-level back button is my #1 grudge with Android (along with all the stuttering)
Mark M.
ah, right, I forgot about the tabs
I'm not a huge fan of navigation within tabs, whether via an OS-level BACK button or your own
I would try to do all of this in a single PreferenceScreen as a result, or not have the preferences be in a tab
stuttering nowadays is mostly from self-inflicted wounds, such as disk I/O on the main application thread, the sorts of things that StrictMode can help identify
Mathias
yes, i did not mean my app specifically, more in general as a user.
but you're right
Mark M.
ah, OK
Mathias
Well, i gotta continue coding, gots to finish this before i get the iphone X tomorrow ,and start updating the IOS app for the darn notch :)
9:30 AM
Mathias
see you around and thanks for the input
Mark M.
happy to be useful!
Mathias
has left the room
10:00 AM
Mark M.
turned off guest access

Yesterday, November 1

 

Office Hours

People in this transcript

  • Mark Murphy
  • Mathias