Search code examples
androidandroid-intentandroid-4.2-jelly-bean

adding an intent to an activity


I am very new to android programming. I am using android 4.2 and I have a main activity which consists of 3 tabs in an actiobBar. I would like to start another activity (a mapActivity), if the user clicked on a certain tab. so I would create an intent if the user clicks on the tab. Here is the code:

     @Override
     public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
         // When the given tab is selected, switch to the corresponding page in
         // the ViewPager.
         mViewPager.setCurrentItem(tab.getPosition());

         if(tab.getText().equals("Map")){

            Intent intent = new Intent().setClass(this, MapDisplayActivity.class);
            this.setIntent(intent);
            startActivity(intent);

    }
}

but whenever I click on "Map", I lose the tabs. It seems like this starts an activity on its own and stops the first one. What I would've liked is to have the map, under the tab named "Map".

Can you help me with giving any hints?


Solution

  • There are two approach here that you are mixing and you can't really mix the two:

    1) you can use Fragments which is what you seem to be using with the actionbar. But then you need to implement the map in a Fragment see http://developer.android.com/reference/com/google/android/gms/maps/SupportMapFragment.html and http://developer.android.com/guide/components/fragments.html

    2) The other way is the old way using separate activities for each tab with TabActivity where you create new Activities like you started to do with the mapActivity, but this way is deprecated as of Honeycomb.