简体   繁体   English

如何在子活动中显示标签?

[英]How to show Tabs in the in th SubActivity?

I make an application in which i had made 5 Tabs on the First Tab there is a ListView When i Click on any ListItem i had call another activity with intent .Now there us a problem As & when i click on Listitem The Tabs will Dissapears How can i show Tabs on my SubActivity . 我制作了一个应用程序,其中我在第一个选项卡上制作了5个选项卡,当我单击任何ListItem时我有一个意图调用了另一个活动。现在有一个问题,当我单击Listitem时,选项卡将消失我可以在SubActivity上显示选项卡吗?

Any help willl be Appretiated. 任何帮助将不胜感激。

That is the normal behavior in Android, not very nice. 这是Android中的正常行为,不是很好。

I usually create the first Activity in a Tab extending from this class 我通常在从此类扩展的Tab中创建第一个Activity

public class TabActivityGroup extends ActivityGroup {
    protected LocalActivityManager manager;
    protected ArrayList<String> mIdList;

    public TabActivityGroup() {
        this(false);
    }

    public TabActivityGroup(boolean single) {
        super(single);
    }

    public void onCreate(Bundle ins) {
        super.onCreate(ins);
        manager = getLocalActivityManager();
        mIdList = new ArrayList<String>();
    }

    public void startChildActivity(String Id, Intent intent) {
        Log.d(this.getClass().getName(), "startChildActivity " + Id + " / " + mIdList.size());
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Window window = manager.startActivity(Id, intent);
        if (window != null) {
            mIdList.add(Id);
            setContentView(window.getDecorView());
        }
    }

    @Override
    public void finishFromChild(Activity child) {
        Log.d(this.getClass().getName(), "finishFromChild("+child.getClass().getName()+") mIdList.size " + mIdList.size());
        int index = mIdList.size() - 1;
        if (index < 1) {
            finish();
        }else{
            Log.e(getClass().getName(), "destroy " + mIdList.get(index));

            manager.destroyActivity(mIdList.get(index), true);
            mIdList.remove(index);
            index--;
            String lastId = mIdList.get(index);
            Activity previous = manager.getActivity(lastId);

            setContentView(previous.getWindow().getDecorView());
        }   
    }

    /**
     * Overrides the default implementation for KeyEvent.KEYCODE_BACK so that
     * all systems call onBackPressed().
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            goBack();
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }


    public void goBack() {
        Log.d(this.getClass().getName(), "goBACK() : " + mIdList.size());
        int length = mIdList.size();
        if (length > 1) {
            Activity current = manager.getActivity(mIdList.get(length - 1));
            current.finish();
        }else{
            ((MyApplication)getApplication()).systemExit(this);
                    //This just calls to System.exit
        }
    }
}

You can then create a Tab like this 然后您可以创建一个这样的标签

public class TabOne extends TabActivityGroup {

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

        startChildActivity(OneActivity.class.getName(), new Intent(this, OneActivity.class));
    }

Things to keep in mind: 注意事项:

  • The tab has nothing inside, just starts the real tab activity with 该标签页内部没有任何内容,只需使用
    the startChildActivity method. startChildActivity方法。 This is to get a nice behavior when going back and reach the first activity and the app needs to close. 当返回并达到第一个活动并且应用程序需要关闭时,这是一种很好的行为。

  • Be very careful with the Context on the subactivities as you have 您必须对子活动的Context非常小心
    to use the TabActivityGroup context. 使用TabActivityGroup上下文。 You can do this manually by 您可以通过以下方式手动执行此操作
    calling getParent() , or better defining a method that loops calling getParent() until you find the root parent. 调用getParent()或更好地定义一个循环调用getParent()的方法,直到找到根父级为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在活动顶部显示“窗口/子活动/对话框”,但如何将焦点保持在活动中 - how to show a “window/subactivity/dialog” on top of an Activity but keeping the focus in the Activity 如何在android中添加tabHost in Subactivity? - How to add tabHost in Subactivity in android? 如何将数组传递给子活动(android)? - How to pass arrays to a subactivity (android)? Android:如何按当前月份显示应用启动时的特定标签,例如如果当前月份为10月,则必须显示第10个标签,依此类推 - Android : How to show specific tabs on startup of app by current month, Example if current month is October, it must be show 10th tab and so on 如何在另一个应用程序中打开一个应用程序的SubActivity - How to open SubActivity of an application inside another application 如何强制主要Acivity在Android中等待子活动? - How to force main Acivity to wait for subactivity in Android? 如何调用另一个应用程序android的subactivity? - how to call subactivity of another application android? 从子活动中调用子活动 - Calling subactivity from subactivity 如何在服务和活动(及其子活动)之间处理IPC? - How to handle IPC between a service and an activity (and its subactivity)? 如何从第三个子活动中调用父活动 - How can I call a parent activity from a third subactivity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM