I am currently developing an android app, I want the back button to go back to the previous page they are on. As I using fragments I am using the,
.addToBackStack("tag")
within the fragment manager transaction to get this affect.
However I have come into problem when I am using the tab host. On one page I have three tabs. Completed Jobs, Active Jobs and Jobs Bid On. On the Jobs bid on page there is a view details button for each job. I want when they go back from this page it to go back to the page with the tab host on but go to the tab it was currently on. At the moment it will go back to "Tab 1" which is Active Jobs when I want it to go to "Tab 2" which is the Jobs Bid on tab as that is the one the button is on and where it came from. I tried using,
.addToBackStack(host.getCurrentTabTag())
but this still sent me back to "Tab 1". Wondered if there was a way to do this easily.
More code can be provided if necessary on request, Thanks.
Edit.
Code that moves it to the next fragment.
fragmentManager.beginTransaction().replace(R.id.content, bidDetailsFragment).addToBackStack("tag").commit();
After fixing another problem in my application, I finally got back to the problem with going back to this. To solve this problem I first checked which methods were being called at different points of opening the page. Here were my findings.
On first Load - 1. On Create 2. On Create View
On Tab Changed - 1. On Tab Changed
Normal Page Load - 1. On Create View
This then gave me the idea of creating a variable which kept check of which tab I was on, this then allowed for whenever the page loaded, it knew which tab I was previously on.
I first created the variable at the top of the page.
private String tabTag;
I then in the On Create Method set it to the default value that I wanted the tabHost to start on.
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
tabTag = "Active";
if (getArguments() != null)
{
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
I then put in a line of code that set the tab host by the tag, this would allow that every time the onCreateView method was called that it would set the tab to whatever the tag was.
host.setCurrentTabByTag(tabTag);
I then finally inside of the on Tab Changed Listener to change the tabTag, to the current tabTag. This allowed for the tabTag String variable to allows keep up with the current tag.
host.setOnTabChangedListener(new TabHost.OnTabChangeListener()
{
@Override
public void onTabChanged(String tabId)
{
tabTag = host.getCurrentTabTag();
}
});
Therefore whenever the back button is clicked, it will run the onCreateView method and set the host by the tag, which on first run will always be the active tag but then after that it will be whatever the last opened tab was.