处理选项卡按下活动组内部以执行反压功能

时间:2015-06-17 10:37:33

标签: android android-tabhost activitygroup

我正在使用5个选项卡实现tabhost。在第5个选项卡上,我有一个包含2个子活动的活动组。从子活动开始,如果我按回按钮,应用程序将返回到父活动。

但我需要的是按下标签按钮,它必须返回到父活动。

这是我的活动小组:

 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());
    }
}

这是我在tabhost活动中处理第二个标签的地方:

  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具有此功能。在当前活动的按Tab键上,它将父活动置于最顶层。请提前给我一些解决方法。谢谢提前!!!

1 个答案:

答案 0 :(得分:0)

此代码用于感知子活动

中的标签按下
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;
        }
    });
}

在子活动中粘贴此代码。如果子活动处于活动状态,则在按下选项卡时它将被关闭,因此父活动将可见。

 @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();
    }
}

如果不清楚,请随意询问。愿意帮忙。