Invoking new activity class when a tab is clicked in actionbarsherlock

user2740002

Folks,

I am new to android development so my question is probably very basic - I am using the tabbed pane from actionbarsherlock and want to have 3 different UI for 3 tabs. However I also need one textbox in common across all tabs. To achieve the first thing my idea was to have 3 different activity classes extending the ActionBar.TabListener but instantiating those classes (using new) from the setTabListener is not working. Any solution?

Regards,

The Main Activity Class -

       ActionBar.Tab aTab = getSupportActionBar().newTab();
    aTab.setText("A");
    aTab.setTabListener(new AActivity());
    getSupportActionBar().addTab(aTab);

    ActionBar.Tab bTab = getSupportActionBar().newTab();
    bTab.setText("B");
    bTab.setTabListener(new MessageActivity());
    getSupportActionBar().addTab(bTab);

    ActionBar.Tab cTab = getSupportActionBar().newTab();
    cTab .setText("C");
    cTab .setTabListener(new DataActivity());
    getSupportActionBar().addTab(cTab );

Now the AActivity Class -

  public class AActivity extends Activity implements ActionBar.TabListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_call);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.call, menu);
    return true;
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    System.out.println("In A");
    TextView txtView = (TextView) findViewById(R.id.aLog);

}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}
 }

It is giving me NullPointer at

 TextView txtView = (TextView) findViewById(R.id.aLog)

so am assuming the new thing is not working at all

Mattias Buelens

You're doing it wrong.

The TabListener interface allows you to respond when a tab is selected or unselected. How you respond to it is completely up to you. It doesn't matter if the implementing class is an Activity or any other particular class - as long as it implements the methods of TabListener in some way or another.

You should never have to instantiate an Activity class yourself through new MyActivity(). Android needs to do that itself so it can set up the environment for the activity. Thus, if you want to launch another activity when a tab is selected, then you should be calling startActivity inside onTabSelected. However, launching an activity when selecting a tab is probably a bad idea. That will open up a completely separate activity (outside of your original tabbed activity) and it won't have the tabbed action bar.

You're looking for fragments: a single activity with a container holding a fragment which you can swap with other fragments. When selecting a tab, you want to replace the current fragment with another fragment specific to that tab. This is demonstrated in the ActionBarSherlock samples, FragmentTabs.TabManager handles the binding between tabs and fragments. To use this in your code, you should:

  1. Make your MainActivity extend SherlockFragmentActivity.
  2. Grab the layout from fragment_tabs.xml.
  3. Grab the TabManager class from FragmentTabs.java and copy it to your codebase (as a separate class or as a static inner class, whatever works).
  4. Copy the setup code from FragmentTabs.onCreate:

    super.onCreate(savedInstanceState);
    
    setContentView(R.layout.fragment_tabs);
    mTabHost = (TabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup();
    
    mTabManager = new TabManager(this, mTabHost, R.id.realtabcontent);
    
  5. Create your tabs with TabManager.addTab, for example:

    mTabManager.addTab(mTabHost.newTabSpec("tabA").setIndicator("A"),
            AFragment.class, null);
    
  6. Make Fragment classes for each tab (e.g. AFragment). Override Fragment.onCreateView, inflate your UI and do some fancy stuff.

Also, read the fragments developer guide!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Open new Tab when button is clicked

From Dev

How to open a new activity when a button is clicked?

From Dev

Proceed to new activity from ListView when clicked

From Dev

Why new activity is opening when notification clicked?

From Dev

New Activity crashes when button is clicked

From Dev

jQuery Add Class on Clicked Tab and Remove When Not

From Dev

Active class to clicked tab

From Dev

Active class to clicked tab

From Dev

Loading new content in a tab when clicked on a button not working

From Dev

Run Audio on new tab when a link is clicked - HTML

From Dev

Youtube/SoundCloud video open in new tab when clicked

From Dev

Start new Activity when html button clicked in Cordova

From Dev

How to Open New Activity when Recycler Items Are Clicked

From Dev

Resume an activity when clicked on a notification

From Dev

What goes into DbContextOptions when invoking a new DbContext?

From Dev

Error : missing new prefix when invoking a constructor

From Dev

How to trigger jquery "click" event even when the user clicked "middle button" or right click -> open in a new tab

From Dev

IE11 does not send session cookie when a link targeting a new tab is clicked (on first request)

From Dev

How to trigger jquery "click" event even when the user clicked "middle button" or right click -> open in a new tab

From Dev

How to retrieve data from firebase in new activity when an item in Listview is being clicked?

From Dev

Android: Start a new activity without showing two activities when Recent Button is clicked

From Dev

An incorrect clicked item in the new activity from RecycleView

From Dev

How to get a JavaScript "class" by invoking "new" from another "class"?

From Dev

Show activities in ActionBarSherlock Tab Navigation

From Dev

How to close previous activity after the notification is clicked and opens a new activity

From Dev

How to start new activity from tab control?

From Dev

Adding new form when clicked

From Dev

Show revelent tab when the respective link is clicked

From Dev

Qt Tab rename when double clicked

Related Related

  1. 1

    Open new Tab when button is clicked

  2. 2

    How to open a new activity when a button is clicked?

  3. 3

    Proceed to new activity from ListView when clicked

  4. 4

    Why new activity is opening when notification clicked?

  5. 5

    New Activity crashes when button is clicked

  6. 6

    jQuery Add Class on Clicked Tab and Remove When Not

  7. 7

    Active class to clicked tab

  8. 8

    Active class to clicked tab

  9. 9

    Loading new content in a tab when clicked on a button not working

  10. 10

    Run Audio on new tab when a link is clicked - HTML

  11. 11

    Youtube/SoundCloud video open in new tab when clicked

  12. 12

    Start new Activity when html button clicked in Cordova

  13. 13

    How to Open New Activity when Recycler Items Are Clicked

  14. 14

    Resume an activity when clicked on a notification

  15. 15

    What goes into DbContextOptions when invoking a new DbContext?

  16. 16

    Error : missing new prefix when invoking a constructor

  17. 17

    How to trigger jquery "click" event even when the user clicked "middle button" or right click -> open in a new tab

  18. 18

    IE11 does not send session cookie when a link targeting a new tab is clicked (on first request)

  19. 19

    How to trigger jquery "click" event even when the user clicked "middle button" or right click -> open in a new tab

  20. 20

    How to retrieve data from firebase in new activity when an item in Listview is being clicked?

  21. 21

    Android: Start a new activity without showing two activities when Recent Button is clicked

  22. 22

    An incorrect clicked item in the new activity from RecycleView

  23. 23

    How to get a JavaScript "class" by invoking "new" from another "class"?

  24. 24

    Show activities in ActionBarSherlock Tab Navigation

  25. 25

    How to close previous activity after the notification is clicked and opens a new activity

  26. 26

    How to start new activity from tab control?

  27. 27

    Adding new form when clicked

  28. 28

    Show revelent tab when the respective link is clicked

  29. 29

    Qt Tab rename when double clicked

HotTag

Archive