I have an Activity having multiple fragments of tabs, on pressing "Running total " button present at the bottom, the UI of that particular tab hides and another layout which displays a table,is shown. I want to display the main UI of the tab and hide that table layout on pressing device's back button. I can't use BackStack as I'm using horizonal naviagtion(tabs). How to achieve this?
You could simply override the onBackPressed() in your activity and write the logic there to handle what you want to do.
If you going to manipulate fragment during onBackPressed, it could be done by overriding onBackPressed in the Activity itself. But this would mess up the Fragment-Activity communication. It's better to have onBackPressed in the Fragment also.
A simple google search will give you multiple links on how to have onBackPressed in Fragment.
Follow the below link for how to implement this: http://vinsol.com/blog/2014/10/01/handling-back-button-press-inside-fragments/
Related
I have a main activity that includes two buttons 'Submit' and 'Pick' and few images. Inside the activity i have two fragments named MyThings and MyPicks. I want to hide activity button 'Pick' in MyThings fragment but want to show in MyPicks fragment. How can i achieve this? Because i don't know how to initialize the button which is in main activity to that fragment so that i can hide and show button in fragments. Please Help
There are several ways to handle this situation.
One of them is that you have two methods in your Activity named
showButton() and hideButton() and in that methods you hide and
show button. In your Fragments you can call these methods in
onCreateView() Method since you can invoke Activity methods from
Fragment.
Another way is you hide or show button in your Activity when
creating and putting Fragment in the container. I prefer the
second way.
I have two xml layout on one activity, when click a button on layout A it will open Layout B but when press back button on layout B it kills the activity. What I want is that when press the back button it will go back to Layout A
I assume you don't know and haven't implemented Fragments in Android, with this I would suggest you to take 2 fragments for a single activity, and you can show/hide a particular fragment as per your requirement.
Then you will have to Override the method of OnBackPressed()
and whatever you want to do on BackPress do it there...
Is it possible to disable an activity elements when it loads a fragments?
I have a program which has an activity and two fragments. I put a container in activity. When I put two buttons in activity and load each fragment by clicking the button, fragment loads on the activity, but when I click in the position of buttons which are under fragment(or in the large screen next to it), they do some actions, however I don't like it. The buttons should not be clickable.
As a simple solution I create a third fragment and put my buttons in it and load it as a default view in the activity.
I was wondering is it possible to do this without using third fragment.
If you do not want clicks to propagate to below layers you can specify android:clickable="true".
In your case define android:clickable="true" in the bottom layout of your fragments layout xml file to stop any clicks to the activity below.
mach's solution is great, but i can suggest a solution that will be helpful if you want to do more actions in the future than just disabling buttons.
You can simply have your activity implement an Interface "OnFragmentLoaded" for example which has a single method onLoaded()
and in your fragment in your onAttach(Activity ac) method you can do the following
((OnFragmentLoaded) ac).onLoaded()
and you activity would implement onLoaded() to do what ever you want
I have many fragments inside my activity. One fragment displays Top Books and when clicking on book this fragment is replaced by fragment showing details of clicked book.
Fragment showing top books should have no title in ActionBar, but drop down navigation with categories. However fragment with details of book should have title in ActionBar.
Here comes my question: how should I handle ActionBar changes in my application. Should I configure ActionBar inside Activity or inside Fragments? If in Activity how can I handle all those changes: back button pressing, up navigation button pressing?
Please help, this situation lacks of good examples.
I'd configure the ActionBar in the Activity.
You can override the onBackPressed() method to do different things depending on what fragment is showing.
This reference shows how to handle the up button being pressed, and you can change the logic depending on what fragment is showing as well: http://developer.android.com/training/implementing-navigation/ancestral.html#NavigateUp
Assuming we have two Activities: List Activity and Detail Activity.
We have a "Save" button in Detail Activity which after saving, finishes the activity to go back to the List Activity.
I would like to know if we can have an alert dialog fired from "Save" function of Detail Activity so that it can stay over the transition of moving back to List Activity..
In other words, Can a Dialog exist outside the Activity? My understanding is that a Dialog is a child of an Activity and has to be destroyed if the activity is destroyed..
In iOS this is possible since the Dialogs are attached to the navigation controllers. Is this possible in Android? I am not considering fragments here..
If you simply want to display a message like "your changes have been saved" then in the Android world you would display a Toast. A Toast can even have a custom layout.
Another possibility would be to move your code to Fragments (which would be a good idea anyway) and then have an activity that acts as controller (i.e. creates and swaps the fragments). That should allow you to open a dialog while activating a different fragment.
You can choose two possible options:
You can stop the AlertDialog when the Detail Activity is dismmissed and then show it again in the method onCreate on the List Activity.
Another option is that you use a one single activity rather than two activities, and change the layout of the Detail Activity to the layout of the List Activity when you push the "Save" button.
There isn't an easier way to get what you want.