2

I have 3 tabs in my sample application with activity group. First tab contains search activity i.e.Home/Root activity and am displaying the results of search in another activity but under same tab i.e Tab1. When I press back button in result activity, it is going to search activity. Everything works fine till here. Now I want to go search activity by pressing tab1 instead of pressing back button. How can achieve this? I tried something like this

public class TabSample extends TabActivity {

    public TabHost tabHost;

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

        this.tabHost = getTabHost();

        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("OPT")
                .setContent(new Intent(this, TabGroup1Activity.class)));

        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("EDIT")
                .setContent(new Intent(this, TabGroup2Activity.class)));

        tabHost.setCurrentTab(1);

        tabHost.setOnTabChangedListener(new OnTabChangeListener() {

            public void onTabChanged(String arg0) {

                if (tabHost.getCurrentTabTag().equals("tab1")) {
                         //What should I do to display search activity here
                } else {
                    tabHost.setCurrentTab(1);
                }

            }
        });

        tabHost.setFocusable(true);
        tabHost.requestFocus();

    }
}

Can anyone please help let me know how to invoke search activity when tab is pressed? What will go into if part? Because if I use tabHost.setCurrentTab(index), it will display result activity but not search activity.

NOTE: I followed the tutorial given in this link.

CrazyCoder
  • 2,465
  • 8
  • 36
  • 57
  • have you tried `movetasktoback(true)` ? – Sarim Sidd Apr 12 '12 at 09:52
  • Updated my answer, to explain a bit more.. – DNRN Apr 12 '12 at 11:33
  • It may be worth looking into Fragments rather than using this method, in the interests of future-proofing (FragmentActivity is deprecated). See http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabs.html as an example. – Ben Neill Apr 19 '12 at 09:14

3 Answers3

0

I'm not 100% sure I understand you fully, but let's see :)

In your onTabChanged listener you can switch on which tab have been tabed, and then open the activity as normal inside an activitygroup:

public void onTabChanged(String tabId) {
    if (tabId.contentEquals("tab1")) {
        Intent intent = new Intent(tabHost.getContext(), TabGroup1Activity.class);
        View view = StartGroup.group.getLocalActivityManager().startActivity("tab1", intent).getDecorView();
        StartGroup.group.setContentView(view);
    }
}

I just reviewed my code and think there's a bit more to explain here. The problem is that you don't stack activities as normal. Instead the workaround is to make a content stack and change these instead. So what I have done is to create a class StartGroup which extends ButtonHandlerActivityGroup:

public class StartGroup extends ButtonHandlerActivityGroup {
// Keep this in a static variable to make it accessible for all the nested  activities, lets them manipulate the view
public static StartGroup group;

// Need to keep track of the history if you want the back-button to work properly, 
// don't use this if your activities requires a lot of memory.  
private ArrayList<View> history;

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

    this.history = new ArrayList<View>();
    group = this;

    // Start the root activity within the group and get its view
    View view = getLocalActivityManager().startActivity("UserList", new Intent(this, UserList.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();

    replaceView(view);
}
public void back() {
    if (history.size() > 0) {
        // pop the last view
        history.remove(history.size()-1);
        setContentView(history.get(history.size()-1));
    } else {
        finish();
    }
}
}

Then from the TabMaster class or what you call it you can use the StartGroup class to change the content view of an activity group.

This is something I wrote to work on devices from 2.2, so there might be an easier and more androidish way to accomplished it, but this works on almost all devices :)

Here is another thread where the use a similar approach: Launching activities within a tab in Android

Let me know if I can help more.

Community
  • 1
  • 1
DNRN
  • 2,397
  • 4
  • 30
  • 48
  • Let me try. Thanks. BTW please let me know, which part of the question you can't able to understand. So that I can modified the question. – CrazyCoder Apr 12 '12 at 09:57
  • I think this will end up with a new copy of the search interface launching over the top of the search results, so if you press back you will end up going to the previous search results, and then back again to the original search form. This would work, but from your description I think what you're looking for is what I posted. – Jules Apr 12 '12 at 10:07
  • How can you have a copy when you switch the contentView..? – DNRN Apr 12 '12 at 10:36
  • Because you're starting a new activity, but leaving the old one in the activity stack beneath it. The content view is not the same thing as the activity, and the ActivityGroup is still managing both of those activity objects in the background. – Jules Apr 12 '12 at 12:19
  • Yes I start an activity to get the decorview, But as you can see I also clears the top in the StartGroup class with the intent flag. So your not right. – DNRN Apr 12 '12 at 12:36
  • @DNRN Let me try and let you know in another 4 hrs – CrazyCoder Apr 12 '12 at 13:19
0

I think what you want to do is this: when the 'tab1' tag is selected, go back to TabGroup1Activity if (and only if) the current activity is not that activity (basically you want to simulate a 'back' press).

If so, what you want is this:

if (getCurrentActivity().getClass() != TabGroup1Activity.class) 
    getCurrentActivity().finish()
Jules
  • 14,841
  • 9
  • 83
  • 130
0

There is an ArrayList in your ActivityGroup so override onPause() method in ActivityGroup and remove all the ids from ArrayList except the first one which must be your SearchActivity. So when you go to other tab then comes back to SearchActivity( or on Tab1 ) Home will be displayed.

Hassan Jawed
  • 1,650
  • 1
  • 12
  • 19