Handling tab press inside a activity group to do back press functionality

205 views Asked by At

I am Implementing tabhost with 5 tabs.On 5th tab i have an activitygroup with 2 child activity.From child activity if i press back button the app returns to the parent activity.

But what i need is on pressing tab button too it has to return to the parent activity.

this is my activity group:

 public class Activitygroup extends ActivityGroup {
private Stack<String> stack;
public static Activitygroup grp;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    grp = new Activitygroup();
    if (stack == null) {
        stack = new Stack<String>();
    }

    push("HomeStackActivity", new Intent(this,Extras.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
}

@Override
public void finishFromChild(Activity child) {
    pop();
}

@Override
public void onBackPressed() {
    pop();
}

public void push(String id, Intent intent) {
    Window window = getLocalActivityManager().startActivity(id,
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    if (window != null) {
        stack.push(id);
        setContentView(window.getDecorView());
    }
}

public void pop() {
    if (stack.size() == 1) {
        finish();
    }
    LocalActivityManager manager = getLocalActivityManager();
    manager.destroyActivity(stack.pop(), true);
    if (stack.size() > 0) {
        Intent lastIntent = manager.getActivity(stack.peek()).getIntent()
                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Window newWindow = manager.startActivity(stack.peek(), lastIntent);
        setContentView(newWindow.getDecorView());
    }
}

this is where i am handling second tab press in tabhost activity:

  int numberOfTabs = tabHost.getTabWidget().getChildCount();
    for (int t = 0; t < numberOfTabs; t++) {
        tabHost.getTabWidget().getChildAt(t).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {

                    String currentSelectedTag = MainActivity.this.getTabHost().getCurrentTabTag();
                    String currentTag = (String) v.getTag();

                    if (currentSelectedTag.equalsIgnoreCase(currentTag)) {
                        MainActivity.this.getTabHost().setCurrentTabByTag(currentTag);
                        String newSelectedTabTag = MainActivity.this.getTabHost().getCurrentTabTag();

                        if (newSelectedTabTag.toLowerCase().indexOf("extras") != -1) {

     "BACKPRESS FUNCTIONALITY"-MUST BRING THE PARENT ACTIVITY ON TOP HERE
                        } 
                        return true;
                    }
                }
                return false;
            }
        });
    }

Iphone has this functionality by default.On pressing tab on current activity it bring the parent activity on top.Please suggest me some workaround for this.thanks in advance!!!

1

There are 1 answers

0
Arun Inbasekaran On

This code is used to sense the tab press from the child activity

int numberOfTabs = tabHost.getTabWidget().getChildCount();
for (int t = 0; t < numberOfTabs; t++) {
    tabHost.getTabWidget().getChildAt(t).setOnTouchListener(new     View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {

                String currentSelectedTag = MainActivity.this.getTabHost().getCurrentTabTag();
                String currentTag = (String) v.getTag();

                if (currentSelectedTag.equalsIgnoreCase(currentTag)) {
                    MainActivity.this.getTabHost().setCurrentTabByTag(currentTag);
                    String newSelectedTabTag = MainActivity.this.getTabHost().getCurrentTabTag();

                    if (newSelectedTabTag.toLowerCase().indexOf("extras") != -1) {

      ****Here call a static method to check whether the child activity is active****
                    childactivity.getappcontext();   
                    } 
                    return true;
                }
            }
            return false;
        }
    });
}

In child activity paste this code.If the child activity is active then it'll be closed when pressing the tab hence the parent activity will be visible.

 @Override
public void onStart() {
    super.onStart();
    active=true;
}

@Override
public void onStop() {
    super.onStop();
    active=false;
}


public static void getAppContext() {
    if(active){
        System.out.println("gallery");
        galleryActivity1.finish();
    }
}

Feel free to ask if not clear.Would love to help.