Opening Fragment From Inside PreferenceScreen - android

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

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.

Related

save layout back into xml

I am working on a project where I have multiple tabs, and I want the content of each tab to be set dynamically based on what is selected in the previous tab. (For my tabs, I am using the SlidingTabLayout with a ViewPager). I currently have my first tab set up correctly and have it tied to an EventBus that creates a LinearLayout based off of my template from R.layouts.tab_2 and adds the content dynamically. What I can't quite figure out is how to save my LinearLayout back into the tab_2.xml file. I have seen suggestions of saving it with SharedPreferences, but that doesn't allow me to save it as the right file type.
If there is a much easier way to do this, such as updating the tab that is already created, I am open to that instead of saving the layout back into the XML file. I am fairly new at programming in Android Studio and still trying to get the basics down.
Your first tab, as I'm interpreting it, is essentially a settings page. The user does a few things, and as a result of those things - the layout on your second tab looks different.
You need to save some variables based on user interactions. For example, if you have a checkbox the user is going to press - you would set a boolean to true when they've checked it, and false when they've unchecked it. When the user navigates to your second tab, you will check the value of that boolean - and alter that layout accordingly.
Hopefully this concept will, even in its simplicity - help you understand how apps are built to be functional.

switch to a new activity, instead of switching fragments, when jfeinstein10's slidingmenu list item is clicked

I have seen a few questions raised on this topic (for e.g.: https://github.com/jfeinstein10/SlidingMenu/issues/5) but I am still unclear. I hope somebody can clarify this.
Context:
See https://github.com/jfeinstein10/SlidingMenu
I have an android app that organizes screens by activities and fragments (i.e.) each screen is an activity containing one or more fragments.
The new requirement is to add a sliding menu (similar to what this library provides).
Issue:
It appears from the examples and discussion that the right model would be to have just 1 MAIN ACTIVITY that will then switch in/out fragments belonging to the different screens. In fact the author mentions in the above thread: "If you were to launch Activities based upon the list selection, then you would not have the behavior where you swap the views that you're talking about. " and also "You can't put an Activity into the above view. That doesn't really make sense when you think about what an Activity is. ".
Why doesn't it make sense? Obviously, I am missing the point here.
Question:
Given that my project already contains multiple activities (one corresponding to each screen), is my only option then to re-organize the project to have JUST 1 MAIN ACTIVITY, in order to use this library? Or alternatively, is there any way to launch a new activity when a list item in the sliding menu is clicked, and still observe the sliding menu behavior, [EDIT- added the last part to be more clear] or in other words, on how exactly to use this library within my existing app design.
Thanks in advance
First, you can't have an Activity inside another and activities are completely different from views as stated in the docs:
An activity is a single, focused thing that the user can do.
Now, to answer your question, it all depends on how you want your app to behave. You could have your activities with the sliding menu implement the onClosedListener and switch to the selected activity from there. This will give you the animation of closing the menu before switching activities. It will also give you a weird effect since every time you select something from your menu you'll see the animation of a new activity coming to the front.
I think the best approach would be to have a "common purpose" between all your sliding menu options. For example, in one of my projects I have to allow the users to select between lists of different types of data. When the user selects anything from the menu, I load a new list fragment into the right corner where he may choose the item he wants to view or edit. That's the app entry point and also the only place were I have a sliding menu in my app. It is pretty much the same for every app that implements this UI design pattern. Look at google+, currents and youtube where the side menu lets you choose which feed or content to show. Once a user makes a selection, just open a new activity for the selected item (a g+ post, a video, a news article, a tweet or whatever it is).
Your app doesn't have to have lists of different data or anything like that to use the sliding menu, but keep in mind that the activity with the sliding menu should have a clear, focused goal with respect to its functionality and purpose. Having a sliding menu because many other apps have one is a bad choice, you should use it with a specific objective. Also keep in mind that applying the sliding menu everywhere would interfere with the platform's navigation pattern and lead to an overall bad user experience since it wouldn't behave as the other apps.
It doesn't make sense to place an Activity into the above view because the Activity is the main controller for the view of each screen. The Activity also shows views and keeps track of Fragments (which in turn are mini controllers, with or without their own views). So placing an Activity in the above view would mean that you would place an Activity in an Activity... Wich is impossible.
From what I can derive from your text I think it would be wise to read through the Android developer guide on Activities and Fragment again (http://developer.android.com/guide/components/activities.html) to get a better understanding of how the concept of Android works.
Now to your question:
I am not clear on what you are trying to achieve but if you want your app, with menu to behave like, say, the Google+ app then one way of doing it is to implement a base class that extends the Activity class (or what ever base Activity used in your project) and let the base set the SlidingMenu. Then you would simple extend your base Activity in each of the Activities that are supposed to have a menu.
You could also do it the way you describe it, but then you would end up with a classic example of a God object (http://en.wikipedia.org/wiki/God_object). It's a neat way to practice your Fragment juggling skills and switching between Fragments instead of starting new Activities does have it's use cases, but I still wouldn't recommend it for a project with more then a few views.
Here is the answer that came closest to the issue I had - http://www.verious.com/article/polishing-the-sliding-app-menu/. Scroll down to the bottom of the page to see the last section titled "Using the fly-in app menu between Activities". This is one option if you have a lot of activities in your existing app and want to avoid extensive re-factoring. I haven't tried this out yet but its worth being aware of.

How to let the user select which Activity to start when application starts?

I want the user to select which Activity the application should start on. I am trying to avoid tabs because I don't like the layout of tabs, but I have a custom title bar that has three icons on which to select functionality/activity.
I want the user to select which activity the application should start from the beginning with a user preferences.
How can I do this?
After starting the main activity, immediately read from the settings and start the user preferred activity.
I also offer an alterntaive soultion. This is not an exact answer to your question though, but may help you to solve your problem in an elegant way.
Subclass TabWidget and define how the row with tabs is drawn.
Create a layout file for TabHost with your TabWidget in it.
Subclass TabActivity and set your own layout you've created.
Read the saved settings and use setDefaultTab method to set the preferred activity.
Voila! You got your own look without losing any functionality of TabActivity. You can read Tab Layout Tutorial for more information on how to do it properly.
What so tough in this. if i have three activity and that i want user to pick one which to start.
at least one activity has to be served as menu to the user from which user will pick. This will be main activity of app. i will display three buttons in it and on their specific click i will initiate the respective activity.

Android: two activities, one tab

What I want to do is the following:
I have 4 tabs in total, but in 1 tab i want to have multiple screens which the user can change using the menu
I can capture the event of when the user presses the right button, so no help needed there:)
what I can't do is starting the Activity without removing the TabBar!
I found some information about changing the view of the framelayout, but i really want to start another activity in the layout, not just change the view (I want to keep my code clean you see;)
thanks in advance
so in Tab A I want to be able to use Activity A and B
The only solution I found is still replacing the view, but doing it through ActivityGroup, so the code would still look normally and would be separated in activities. Works great, but there still is needs for hacks like back button press and stuff.
Scrapped this idea, though, since later I wanted more flexible and more stylish tab bar, so I wrote my own. Works like a charm and took some 2 working days to code.

Modifying application workflow to use TabActivity

This question actually has two parts.
The first part:
I've been developing my first app for a couple of weeks now. I have 5 screens and everything seems well. However, I'm considering changing the app's navigation to a TabView.
I haven't delved much into it, but I'm hoping someone can save me a little bit of time. It seems that people don't generally place Activities inside each tab. They simply point the tab content to a View. This is where my major setbacks are. 1) I already have Activity classes full of code and 2) I can't quickly guess how the structure of an app using TabView looks. For example, where do I put the handler code for clicking a button on a View? Does it all just get dumped into the TabView Activity somehow?
What I would like is if you could please give me a quick synopsis of what I'm looking at doing, answers to any questions you think I may have, and point me toward some resources for creating TabView applications. A quick Google search really just shows me how to create a TabView Activity and add a couple tabs to it. The code doesn't go any deeper. For example, say I have a layout xml to show in one of my tab's content pane, where does the code go for clicking a button I have in that layout?
The second part:
I've added a TabActivity to wrap the Activities I currently have in. At the moment I have Activities populating the content of my tabs (though ultimately I'd like to do this in the most efficient fashion, which doesn't seem to be having Activities be tab content). I've noticed something rather annoying. My MAIN Activity is an Activity I wrote for my user to log in to their account. After logging in, they are taken to my Tab Activity. Here is what happens:
When I am on my Tab Activity and I "minimize" the app by clicking the Home button and then launch it again, I don't get taken back to the Tab Activity. I get taken to my log in Activity. Why? I don't have the launchMode of my Tab Activity set to singleInstance... or is it singleInstance by default? How can I make the app re-launch showing the Tab Activity (ideally by setting some parameter, assuming I'm doing something wrong, and not having to save this data off somewhere and reading it and programmatically telling it what to go to)?
Thank you for all your time and help
I don't have a comment on the advisability avoiding the use of sub-activities in TabActivity. As for handlers -- if you aren't going to embed views instead of activities, then all the android:onclick type handler settings in your layout XML will call methods on the TabActivity. This is because they go to methods on the views' Context, which is the generally the nearest containing Activity. If you want to split your code up further without using Activities, I believe you'll have to use findViewById calls on the tab content views after you've set them up, and bind the handlers manually from there in your code.

Categories

Resources