如何从ActivityGroup堆栈中删除所有活动,并在选项卡单击事件的当前选项卡中加载第一个活动

时间:2013-05-31 06:41:47

标签: android android-tabhost activitygroup android-tabactivity

我很久以前使用TabActivity和ActivityGroup创建了一个项目(现在已弃用)。

问题可能很简单,但首先我想澄清一下这个问题。我的Tabhost中有4个选项卡。我使用以下代码来跟踪堆栈活动。

MyActivityGroup.java

public class MyActivityGroup extends ActivityGroup {
    public static Stack<String> stack;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (stack == null) {
        stack = new Stack<String>();
    }

    push("1stStackActivity", new Intent(this, Home.class));

}

@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()
                .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Window newWindow = manager.startActivity(stack.peek(), lastIntent);
        setContentView(newWindow.getDecorView());
    }
}
}

请注意:

push("1stStackActivity", new Intent(this, Home.class));

Home.class是我在HOME TAB中加载的第一个活动。

这是一张显示我的流程和问题的图片。

enter image description here

单击按钮时从Activity A开始(浏览图操作A)。我开始这样的新活动

Intent intent = new Intent();
            intent.setClass(getParent(), BookOverView.class);

            MyActivityGroup activityStack = (MyActivityGroup) getParent();
            activityStack.push("2ndStackActivity", intent);

这会在当前Activity B上加载HOME TAB,同时也会将活动推送到堆栈中。

当我单击任何列表项时,从活动B开始我在同一个选项卡中开始一个新活动,其代码与上面的(3rdStackActivity)相同。它开始我的Activity c;当我按回KEY时它工作正常(即来自“活动C” - >“活动B” - &gt; ACTIViTY A)。

问题

站在activity C当我点击Home TAB时(参见图C),它应该回到我的第一个活动(即活动A)。

这是我的HOME TAB点击事件

getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if (getTabHost().getCurrentTab() == 0) {

                                    \\ Check If stack has at least more then one activity loaded.
                if (MyActivityGroup.stack.size() > 1) {

                                     //My code to clear the stack and load the 1st Activity goes here


                                     // I tried Here to clear my stack 
                                     // But not sccessfull



                }

            } else {

                getTabHost().setCurrentTab(0);

            }
        }
    });

我在尝试清理Stack并开始第一个家庭课时遇到了问题。

这是我的MainActivity.java,它扩展了TabActivity

public class MainActivity extends TabActivity {

Context context = MainActivity.this;

TabHost tabHost;
TabSpec spec;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tabHost = getTabHost();
    // Android tab
    tabHost.addTab(tabHost
            .newTabSpec("Home")
            .setIndicator("Home",
                    getResources().getDrawable(R.drawable.home))
            .setContent(
                    new Intent(this, MyActivityGroup.class)
                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

    tabHost.addTab(tabHost
            .newTabSpec("Now Reading")
            .setIndicator("Now Reading",
                    getResources().getDrawable(R.drawable.now_reading))
            .setContent(
                    new Intent(this, NowReadingActivityGroup.class)
                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

    tabHost.addTab(tabHost
            .newTabSpec("Favorites")
            .setIndicator("Favorites",
                    getResources().getDrawable(R.drawable.favorites))
            .setContent(
                    new Intent(this, FavoriteActivityGroup.class)
                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

    tabHost.addTab(tabHost
            .newTabSpec("Profile")
            .setIndicator("Profile",
                    getResources().getDrawable(R.drawable.profile))
            .setContent(
                    new Intent(this, ProfileActivityGroup.class)
                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

    tabHost.setCurrentTabByTag("Home");

    tabHost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {

            Toast.makeText(context, "Changed", Toast.LENGTH_SHORT).show();

        }
    });

}

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();

    getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if (getTabHost().getCurrentTab() == 0) {

                if (MyActivityGroup.stack.size() > 1) {


                }

            } else {

                getTabHost().setCurrentTab(0);

            }
        }
    });

}



}

编辑2

我在onTabClick事件中已经这样做了

        if (MyActivityGroup.stack.size() > 1) {

                    while (!MyActivityGroup.stack.isEmpty()) {
                        MyActivityGroup.stack.pop();
                    }

                    Intent homeIntent = new Intent();
                    homeIntent.setClass(MainActivity.this, Home.class);
                    MyActivityGroup activityStack = (MyActivityGroup) getCurrentActivity();

                    activityStack.push("HomeActivity", homeIntent);
                }

            } else {

                getTabHost().setCurrentTab(0);

            }

这会加载我的第一个活动并清除我的活动堆栈。但问题是,现在每当将我的标签更改为任何其他标签并返回到主页选项卡并按回按钮时它就会崩溃。这是日志猫。

05-31 14:15:42.769: E/AndroidRuntime(550): FATAL EXCEPTION: main
05-31 14:15:42.769: E/AndroidRuntime(550): java.lang.NullPointerException
05-31 14:15:42.769: E/AndroidRuntime(550):  at my.islamic.books.lib.app.MyActivityGroup.pop(MyActivityGroup.java:120)
05-31 14:15:42.769: E/AndroidRuntime(550):  at my.islamic.books.lib.app.MyActivityGroup.finishFromChild(MyActivityGroup.java:30)
05-31 14:15:42.769: E/AndroidRuntime(550):  at android.app.Activity.finish(Activity.java:3259)
05-31 14:15:42.769: E/AndroidRuntime(550):  at android.app.Activity.onBackPressed(Activity.java:1920)
05-31 14:15:42.769: E/AndroidRuntime(550):  at android.app.Activity.onKeyUp(Activity.java:1898)
05-31 14:15:42.769: E/AndroidRuntime(550):  at android.view.KeyEvent.dispatch(KeyEvent.java:1280)
05-31 14:15:42.769: E/AndroidRuntime(550):  at android.app.Activity.dispatchKeyEvent(Activity.java:2078)
05-31 14:15:42.769: E/AndroidRuntime(550):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1663)
05-31 14:15:42.769: E/AndroidRuntime(550):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
05-31 14:15:42.769: E/AndroidRuntime(550):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
05-31 14:15:42.769: E/AndroidRuntime(550):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1687)
05-31 14:15:42.769: E/AndroidRuntime(550):  at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1120)
05-31 14:15:42.769: E/AndroidRuntime(550):  at android.app.Activity.dispatchKeyEvent(Activity.java:2073)
05-31 14:15:42.769: E/AndroidRuntime(550):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1663)
05-31 14:15:42.769: E/AndroidRuntime(550):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
05-31 14:15:42.769: E/AndroidRuntime(550):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
05-31 14:15:42.769: E/AndroidRuntime(550):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
05-31 14:15:42.769: E/AndroidRuntime(550):  at android.widget.TabHost.dispatchKeyEvent(TabHost.java:278)
05-31 14:15:42.769: E/AndroidRuntime(550):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
05-31 14:15:42.769: E/AndroidRuntime(550):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
05-31 14:15:42.769: E/AndroidRuntime(550):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1687)
05-31 14:15:42.769: E/AndroidRuntime(550):  at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1120)
05-31 14:15:42.769: E/AndroidRuntime(550):  at android.app.Activity.dispatchKeyEvent(Activity.java:2073)
05-31 14:15:42.769: E/AndroidRuntime(550):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1663)
05-31 14:15:42.769: E/AndroidRuntime(550):  at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2560)
05-31 14:15:42.769: E/AndroidRuntime(550):  at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2535)
05-31 14:15:42.769: E/AndroidRuntime(550):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1867)
05-31 14:15:42.769: E/AndroidRuntime(550):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 14:15:42.769: E/AndroidRuntime(550):  at android.os.Looper.loop(Looper.java:123)
05-31 14:15:42.769: E/AndroidRuntime(550):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-31 14:15:42.769: E/AndroidRuntime(550):  at java.lang.reflect.Method.invokeNative(Native Method)
05-31 14:15:42.769: E/AndroidRuntime(550):  at java.lang.reflect.Method.invoke(Method.java:507)
05-31 14:15:42.769: E/AndroidRuntime(550):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-31 14:15:42.769: E/AndroidRuntime(550):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-31 14:15:42.769: E/AndroidRuntime(550):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我在HomeTab点击事件中已经这样做了

getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if (getTabHost().getCurrentTab() == 0) {

                Log.i("not Null", "Not null");

                if (MyActivityGroup.stack.size() > 1) {

                    while (!MyActivityGroup.stack.isEmpty()) {
                        MyActivityGroup.stack.pop();
                    }

                    Intent homeIntent = new Intent();
                    homeIntent.setClass(MainActivity.this, Home.class);
                    MyActivityGroup activityStack = (MyActivityGroup) getCurrentActivity();

                    activityStack.push("HomeActivity", homeIntent);
                }

            } else {

                getTabHost().setCurrentTab(0);

            }

        }

    });

同样在tabchange listner上我还要清除我的activityGroup堆栈。所以我做了我喜欢这个

tabHost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {

            Toast.makeText(context, "Changed", Toast.LENGTH_SHORT).show();
            while (!MyActivityGroup.stack.isEmpty()) {

                if (MyActivityGroup.stack.size()==1) {
                    break;
                }
                MyActivityGroup.stack.pop();
            }


        }
    });
嗯最后我发现了我的头部药丸:)