AndroidBar Tab on Fragments/EU4You_6

665 Views Asked by At

How are you Mark? I have a question regarding the program you have written in the book "CommonsWare The Busy Coders Guide to Android Development".

I am talking about Fragments/EU4You_6 on Chapter 28 page 377. I want to expand this by adding an ActionBar Tab.

Without making any changes on your original program, what I did was that I copied EU4You.java to EU5You.java, which represents Tab2. EU4You.java will be the default Tab1.

The following are my approach:

I created a java program called EU4Main.java, which represents the MAIN program instead of the original EU4You. Of course, I changed the manifest to android:name=".EU4Main"

The EU4Main.java is where I put the ActionBar Tab. The trouble is and making me frustrated is in the TabListener setup. I have this setup .setTabListener(new TabListener(EU4You.class))); , which passes a Class. It did not work. Do you have any advise on this instead of passing a class?

Also, the onTabSelected on the code snippets below, did not work properly. If I clicked Tab2, it will show the list for Tab2 but it will automatically returns to Tab1. It won't stay at Tab2. I don't know why?

I would appreciate of any help if you can provide a better and working approach for both .setTabListener and onTabSelected

I have also extended FragmentActivity to EU4Main or shall I just use extends Activity instead?

Thanks in advance.

I have included EU4Main below with incorrect and incomplete codes( I just can't make it work...)

public class EU4Main extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final ActionBar bar = getActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

        bar.addTab(bar
                .newTab()
                .setText("Countries")
                .setTabListener(new TabListener(EU4You.class)));

        bar.addTab(bar
                .newTab()
                .setText("Artists")
                .setTabListener(new TabListener(EU5You.class)));
.
.
.
private class TabListener implements ActionBar.TabListener {


public TabListener(Activity activity) {
             mActivity = activity;

        }


  public void onTabSelected(ActionBar.Tab tab, android.app.FragmentTransaction unused) {
          if (tab.getPosition() == 0) {
            Intent intent = new Intent();
                String packageName = "com.commonsware.android.eu4you";
                String className   = "com.commonsware.android.eu4you.EU4You";
                intent.setClassName(packageName, className);
                startActivity(intent);
else{
            Intent intent = new Intent();
                String packageName = "com.commonsware.android.eu4you";
                String className   = "com.commonsware.android.eu4you.EU5You";
                intent.setClassName(packageName, className);
                startActivity(intent);

}

        }


 public void onTabUnselected(ActionBar.Tab tab, android.app.FragmentTransaction unused) {
          FragmentManager fragMgr = getSupportFragmentManager();
          FragmentTransaction xaction=fragMgr.beginTransaction();

        }
     public void onTabReselected(ActionBar.Tab tab,
                                    android.app.FragmentTransaction xaction) {
          // NO-OP
        }
}
1

There are 1 best solutions below

2
On

Your TabListener has a constructor that takes an Activity. You are calling the constructor with a Class. A Class is not an Activity.

Furthermore, you are using two separate TabListener instances, but your code for TabListener does not do anything different based upon the supplied parameter.

And, you are starting activities when tabs are selected, which is not going to be especially useful.

When a TabListener is called with onTabSelected(), it needs to affect a change to the existing UI. Starting a whole new activity does not constitute a change to the existing UI. Rather, TabListener should do something like:

  • execute a FragmentTransaction
  • set a fresh ListAdapter in the ListFragment managed by the tabs
  • update ordinary widgets in the current activity
  • etc.