Reputation: 371
I want to include Action Bar Tabs in my application and am working through a tutorial example to get my head round it all.
The tutorial has two tabs each with their own fragment so then when the tab is selected the correct fragment is shown...this seems to be the approach shown in all the examples I can find.
What I want to do however is to have each tab have their own instantiation of the Same fragment and to pass an argument to the Fragment so that the code can behave differently dependent on which tab is selected.
This is the code from the tutorial:
ACTIVITY (Start Activity)
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
appContext = getApplicationContext();
//ActionBar
ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab PlayerTab = actionbar.newTab().setText("Fragment A");
ActionBar.Tab StationsTab = actionbar.newTab().setText("Fragment B");
Fragment PlayerFragment = new AFragment();
Fragment StationsFragment = new BFragment();
PlayerTab.setTabListener(new MyTabsListener(PlayerFragment));
StationsTab.setTabListener(new MyTabsListener(StationsFragment));
actionbar.addTab(PlayerTab);
actionbar.addTab(StationsTab);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menuitem_search:
Toast.makeText(appContext, "search", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuitem_add:
Toast.makeText(appContext, "add", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuitem_share:
Toast.makeText(appContext, "share", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuitem_feedback:
Toast.makeText(appContext, "feedback", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuitem_about:
Toast.makeText(appContext, "about", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuitem_quit:
Toast.makeText(appContext, "quit", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
}
}
The following class is included at the end in the same java file :
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
public void onTabReselected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
Toast.makeText(StartActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
}
public void onTabSelected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
public void onTabUnselected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
ft.remove(fragment);
}
This is A FRAGMENT:
public class AFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.afragment, container, false);
}
}
This is B FRAGMENT
public class BFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.bfragment, container, false);
}
}
HERE ARE THE XML FILES:
Main.xml
<LinearLayout
android:orientation="vertical"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
A Fragment:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/textView1"
android:text="Fragment A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
B Fragment:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/textView2"
android:text="Fragment B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
So what I would like to do would be to get rid of Fragment B completely and have both Station and Player tabs use Fragment A. Then within Fragment A put code that did one thing if it is for Station and something else if it is for Player. I've tried all sorts with trying to put an Args Bundle but I'm new to this and nothing I have tried has worked,
Upvotes: 1
Views: 976
Reputation: 371
I now have a solution that works here. The way that I have done it is to set a Tag on each of the Tabs as I set them up and then get the current Tab in my fragment and use it's tag.
Code changes are as follows:
In Activity, so that I am just using Fragment A:-
ActionBar.Tab PlayerTab = actionbar.newTab().setText("Tab A")
.setTag("A");
ActionBar.Tab StationsTab = actionbar.newTab().setText("Tab B")
.setTag("B");
Fragment PlayerFragment = new AFragment();
Fragment StationsFragment = new AFragment();
Now within Fragment A I have overriden onResume() and put in the following code :-
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
TextView v = (TextView)getView().findViewById(R.id.textView1);
Tab b = getActivity().getActionBar().getSelectedTab();
String c = b.getTag().toString();
if (c.equals("A"))
v.setText("Tab A has been selected");
if (c.equals("B"))
v.setText("It's Tab B now");
}
Upvotes: 1