psycotica0
psycotica0

Reputation: 3460

Opening Fragment From Inside PreferenceScreen

sorry if this is a stupid question.

So, I've got a settings.xml defining my preferences, and at some point in the tree I have:

<PreferenceScreen [...] android:fragment="path.to.fragment" [...] />

And when I tap that thing, I go to my fragment. That's working.

My fragment is a ListFragment, because I am showing a list of Bluetooth devices on this screen and can't just declare them statically.

My question is, when a user clicks one of the list items in my ListFragment, I want to open a details screen where they can set a preference with that selected device, but I can't figure out how to programatically create and launch this next fragment while still keeping things in the nice tree the preferences provides.

For now I'm just creating an activity so that I can move past that part of the issue, but I have to figure something out before this is done.

Is there an easy way to do this, or am I taking the wrong approach entirely?

Thanks!

EDIT: Picture

help picture

So, Page 1 is my settings fragment, which is built using the XML with the highlighted option being generated using PreferenceScreen as above.

Page 2 is my listFragment, and because those items come from Bluetooth, it can't just be another settings xml entry.

Page 3 is what I get when I pick an option (WIP). To get to this point I had to make it an activity and use startActivity, but the other two were fragments. I'm asking if there's a way to programmatically do the same thing the PreferenceScreen option is doing in the xml and launch a fragment with the top bar maintained and stuff.

Upvotes: 2

Views: 2315

Answers (1)

psycotica0
psycotica0

Reputation: 3460

Alright, here's what I've got.

I realized that at the top level I had a PreferenceActivity, and it has the method startPreferenceFragment.

So, I do:

((PreferenceActivity) getActivity()).startPreferenceFragment(new MyFragment(), true);

Which works for me.

It doesn't feel like a solid solution, but it has the effect I want and I have to move on.

Upvotes: 4

Related Questions