For example, I have 5 tab inside a fragment tabhost.
For my past experience, if I have to implement a "go to detail" page inside a tab, I would just simply use activity to do (e.g. intent and create new activity).
Since I would like to keep the tabhost bar at the page so I can not use activity anymore , I need to change the fragment which is inside the tabhost. I tried using fragment transaction but when I switch tab the text is overlap, so how to change the fragment inside the tab? Thanks a lot.
This is how I create tabhost
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
tabHost.addTab(
tabHost.newTabSpec("home").setIndicator("",
getResources().getDrawable(R.drawable.btn_home)),
HomeFragment.class, null);
tabHost.addTab(
tabHost.newTabSpec("exhibit").setIndicator("",
getResources().getDrawable(R.drawable.btn_exhibit)),
ExhibitFragment.class, null);
tabHost.addTab(
tabHost.newTabSpec("plan").setIndicator("",
getResources().getDrawable(R.drawable.btn_floor_plan)),
PlanFragment.class, null);
tabHost.addTab(
tabHost.newTabSpec("collection").setIndicator("",
getResources().getDrawable(R.drawable.btn_collection)),
CollectionFragment.class, null);
tabHost.addTab(
tabHost.newTabSpec("setting").setIndicator("",
getResources().getDrawable(R.drawable.btn_setting)),
SettingFragment.class, null);
This is how I go to detail before, I would like to use fragment now
private OnClickListener set_listener(final int pos) {
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ctx, CollectionDetail.class);
intent.putExtra("pos", pos);
startActivity(intent);
}
};
return listener;
}
Related
I want to know if there is a way that I start an activity instead of launching a fragment on tabhost tab select.
What I want to acomplish if to launch an activity where the user can write a post and then save, why I want this? because when I want to edit I launch an activity and I don't want to have 2 classes that do the same thing with a little difference, and also the tab on the bottom uses so much space that leave my form area tiny.
My code actually looks like
#Override
protected void onCreate(Bundle savedBundleState) {
mTabHost.setup(getBaseContext(), getSupportFragmentManager(), R.id.realtabcontent);
// This starts the HomeFragment
mTabHost.addTab(newTabSpec("tabHome", R.layout.tab_home), HomeFragment.class, null);
// This throws an error
mTabHost.addTab(newTabSpec("tabPost", R.layout.tab_post));
mTabHost.setOnTabChangedListener(this);
}
private TabSpec newTabSpec(String tag, int layout) {
View view = getLayoutInflater().inflate(layout, null);
return mTabHost.newTabSpec(tag).setIndicator(view);
}
#Override
public void onTabChanged(String tabId) {
if (tabId == "tabAddPublication") {
startActivity(new Intent(getBaseContext(), PostActivity.class));
}
}
I got this error
you must specify a way to create a tab content
Regards
From the Documents
public TabHost.TabSpec setContent (Intent intent)
Specify an intent to use to launch an activity as the tab content.
You need to set the content for TabSpec
Intent postActivityIntent = new Intent(this, PostActivity.class);
mTabHost.newTabSpec(tag).setContent(postActivityIntent);
Hope this helps.
I am using FragmentTabHost in my app. I have three tabs. Each tab shows a Fragment.
addTab("Tab1", R.drawable.ic_launcher, Fragment1.class);
addTab("Tab2", R.drawable.ic_launcher, Fragment2.class);
addTab("Tab3", R.drawable.ic_launcher, Fragment3.class);
addTab("Tab3", R.drawable.ic_launcher, Fragment4.class);
When I press back from any of these tabs, the app is closed and home screen is shown. Now what I want is, when I press back from Tab1, the app should close. However, if I press back from Tab2 or Tab3, the user should be sent to Tab1. Summary is:
Currently in Tab1 -> press back -> app close
Currently in Tab2 -> press back -> Go to Tab1
Currently in Tab3 -> press back -> Go to Tab1
How can I achieve this?
In your Activity (where you have your FragmentTabHost) override the onBackPressed(). In onBackPressed() you can check for the currentTab's position.
If current tab is not 0 (i.e. not the first tab) then set the previous tab as the current tab.
Else if current tab is 0, exit the app.
Since i do not have your actual class, I created a dummy class with FragmentTabHost just to demonstrate how it can be done.
public class MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost) findViewById(R.id.tab_host);
mTabHost.setup(this, getSupportFragmentManager(), R.id.tab_framelayout);
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator("Tab1",
getResources().getDrawable(R.drawable.ic_launcher)),
Fragment1.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator("Tab2",
getResources().getDrawable(R.drawable.ic_launcher)),
Fragment2.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab3").setIndicator("Tab3",
getResources().getDrawable(R.drawable.ic_launcher)),
Fragment3.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab4").setIndicator("Tab4",
getResources().getDrawable(R.drawable.ic_launcher)),
Fragment4.class, null);
}
#Override
public void onBackPressed() {
//get current tab index.
int index = mTabHost.getCurrentTab();
//decide what to do
if(index!=0){
mTabHost.setCurrentTab(index-1);
} else {
super.onBackPressed();
}
}
}
I am confused about moving between Fragments in a TabHost. I have following TabHost inside Activity:
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(),
R.id.layout_main_tab_content);
tabHost.addTab(tabHost.newTabSpec(FragmentA.TAG)
.setIndicator("first"), FragmentA.class, null);
tabHost.addTab(
tabHost.newTabSpec(FragmentB.TAG).setIndicator(
"second"), FragmentB.class, null);
If the tab in changed what exactly happens? What happens to the previous Fragment and the changed one? What is the transaction? Add/remove or replace?
Is there any way I can get (find) the Fragment from initialized tab? And how about restoring the previous fragment when I return to the tab (can I add the transaction to the backstack)?
I created three tabs using FragmentTabHost. Now I need to make all the navigation are under the tabs. How can I get this. I need to get what we get using TabGroupActivity.
Tab1
--->frag1-->frag2
Tab2
--->frag3
Tab3
--->frag4--->frag5
I have used fragmentTransaction.add(), fragmentTransaction.remove(), fragmentTransaction.replace(). These three methods but nothing give solution.
Replace method shows new fragment view on top of existing fragment view.
Remove and Add, from these two remove only works, add does not works.
Thanks in advance.
TabHostMain.java
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.bottom_tabs);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
View view;
view=getTabView(R.drawable.ic_launcher);
Bundle b = new Bundle();
b.putString("key", "Simple");
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator(view),Fragment1.class, null);
b = new Bundle();
b.putString("key", "Contacts");
view=getTabView(R.drawable.ic_launcher);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator(view), Fragment2.class, null);
b = new Bundle();
b.putString("key", "Custom");
view=getTabView(R.drawable.ic_launcher);
mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator(view),Fragment3.class, null);
}
Fragment3.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view=LayoutInflater.from(getActivity()).inflate(R.layout.activity_second, null);
((TextView)view.findViewById(R.id.second_act_text)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fm=getChildFragmentManager();
FragmentTransaction fragmentTransaction=fm.beginTransaction();
fragmentTransaction.replace(R.id.second_activity, new Fragment1()).addToBackStack(null).commit();
}
});
return view;
}
The concept of different fragment navigation stacks in each tab has been discussed quite a few times on stackoverflow, one example for instance:
Separate Back Stack for each tab in Android using Fragments
One simple/crude way of achieving this without having to manage your own custom navigation back stack is to have a root fragment under each tab, and then whenever a root fragment wants to navigate to another fragment (fragment B) simply show a new Activity initially with fragment B and that Activity will have its own fragment navigation back stack.
Tab1 --->root frag1 --> Activity (own nav back stack) --> frag2
Tab2 --->root frag3
Tab3 --->root frag4 --> Activity (own nav back stack) --> frag5 --> frag6 --> frag7
An example of an app that does something like this is actually the StackAnywhere app. It makes heavy use of tabs, but when you navigate within those tabs it generally moves the navigation to a new Activity. YMMV with this approach, however.
I use a TabHost.
The below code to call AActivity.
intent = new Intent().setClass(this, AActivity.class);
spec = tabHost.newTabSpec("A").setIndicator("A", res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);
And it is in the tab.
But in AActivity I call BActivity.
The BActivity will open new page, but not in the tab.
How to let it on the tab frame?
AActivity use below code to call BActivity:
it = new Intent(this, BActivity.class);
startActivity(it);
If you want to open multiple activity in Tab then on Place of activity use Activity group for par tab and switch view in this activity group for open multiple Activity in single tab
you can take some help from this tutorial
You need to change the current selected tab. There's a method called setCurrentTabByTag(String tag) in the TabHost class that will do that, just pass the tag name of your tab (which in your case is B), or you can use the setCurrentTab(int index) and pass the tab index.
Example. Usually I have a MainActivity class, which is my TabActivity. Inside of this class, I will create all tabs that I need on the onCreate method.
First I set some static int with the tabs indexes.
// Tab index.
public static int FIRST_TAB = 0;
public static int SECOND_TAB = 1;
Later, I create my tabs in the onCreate method.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Setting the content view.
setContentView(R.layout.main);
// Getting the TabHost object.
mTabHost = getTabHost();
// Declaring the Intent object for the tabs.
Intent intent;
// Creating the First tab.
intent = new Intent().setClass(this, MyFirstActivity.class);
addTab(mTabHost, "First", FIRST_TAB, intent)
// Creating the Second tab.
intent = new Intent().setClass(this, MySecondActivity.class);
addTab(mTabHost, "Second", SECOND_TAB, intent);
// Setting the current tab.
switchTab(FIRST_TAB);
}
public void addTab(TabHost host, String title, String tag, Intent intent) {
TabHost.TabSpec spec = host.newTabSpec(tag);
spec.setContent(intent);
spec.setIndicator(title);
host.addTab(spec);
}
And last the method that will change the current tab.
public void switchTab(int index) {
mTabHost.setCurrentTab(index);
}
Later, inside of the MyFirstActivity you can call the MainActivity swichTab method and pass the index of the tab to change it.
You can retrieve the MainActivity calling the getParent() method of the Activity class.
MainActivity parent = (MainActivity)getParent();
In the tab activity class where the tabhost is created, implement the following method.
public void switchTab(int tab){
tabHost.setCurrentTab(tab);
}
In AActivity/BActivity implement the following method and call it on any event(that you need):
public void switchTabInActivity(long indexTabToSwitchTo){
TabActivity tabActivity;
tabActivity = (TabActivity) this.getParent();
tabActivity.switchTab(indexTabToSwitchTo);
}
Here TabActivity is the class where tabhost is created