Search code examples
android-intenttabsandroid-tabhost

startActivity from activity in tabContent area of tabHost


I have an app that uses TabHost and loads the different activities into the TabContent area of the layout. If I click on a tab, a new intent is fired off that starts the activity. All is working well, however I'm curious how I can handle the following. Basically if I click for example contacts, the contacts.java class is loaded and my contacts.xml layout file is used for setContentView. However, if I click a button inside of my contacts.xml layout, say "Admin Contact", I need the intent to load within that same tabcontent, and it seems to just open as it's own intent and not within the tabcontent area. Is there something I'm missing with the way TabHost is meant to work? Should I have one contacts.java class and simply set different content views instead of actually starting a new activity? I have a header image and the tabs defined in my main.xml layout and my main.java is the activity which contains the tabhost. I hope my question is clear, any help is greatly appreciated!!!


Solution

  • http://www.mkyong.com/android/android-tablayout-example/ check this example and then for each parent Activity i.e If you have a tab with contacts and profile. Then contacts and profile will be your parent activity.So extend that parent activity to ActivityGroup and make a static context of that activity and for calling an intent in that activity use

     Intent intent = new Intent(ctx, c);
        View subView = getLocalActivityManager().startActivity(id,
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
        replaceView(subView);
    }
    
    public void replaceView(View subView) {
        viewStackList.add(subView);
        setContentView(subView);
    }
    

    where c is the class you want to switch to and viewStackList is an arraylist of type View. this arraylist is used for maintaing stack for back clicks.