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...
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 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/
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 a form, and I have chosen to divide it in two steps.
To do it, I created two layouts for a same activity. When the user complete the first form, I call the second layout with :
setContentView(R.layout.activity_form2);
The problem is, if the user wants to come back at the first step of the form, it's not running, because he comes back to the previous activity.
Is it correct to do that, or I need to use fragment?
Otherwise, how can I do to come back on the previous layout, and not the previous activity?
Never set different layout's for the same Activity. You can navigate to a different Activity or you could use Fragments.
Layout is set to the Activity and when you click back button Activity is popped from the back stack and previous Activity in the stack takes focus. So setting different layouts for the same Activity is not a good choice.
I have used 2 tabs in my app.
when i click on first tab it is showing an list view( default) and when i click on any particular item in list view it is taking me to the next activity to display the selected data. Here when i click on tab again i want that i should go to the listview screen which is default screen.
but nothing happens on clicking the tab but when i press the back button i m going back on the screen. but i want to go to home list view screen on tab click,
pls help me in this case...
You say you start a new Activity. This one can not call the first Activity where you have the tabs. Am I right that your new activity just shows the two tabs as well?
What you could do is the following:
In the second Activity react to the onclick of the tab. When clicked call finish() to close the Activity.
I am pretty sure, that you don't need to do the following. But I write it, if you need it for something in the future :).
In the first Activity call the next through startActivityForResult(...)
In the first Activity overwrite the onActivityResult method. there you can switch to the tab with the list.