具有BroadcastReceiver的TabActivity子活动在后台保持活动状态

时间:2012-11-01 19:23:03

标签: android broadcastreceiver tabactivity

我有一个名为MyTabActivity的TabActivity,其中包含我使用此代码创建的几个选项卡:

public class MyTabActivity extends TabActivity {

    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.my_tabs);
        tabHost = (TabHost)findViewById(android.R.id.tabhost);
        tabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
        Intent intent = new Intent().setClass(this, MyTab1.class);
        setupTab(new TextView(this), "Tab1", intent);
        intent = new Intent().setClass(this, MyTab2.class);
        setupTab(new TextView(this), "Tab2", intent);
        intent = new Intent().setClass(this, MyTab3.class);
        setupTab(new TextView(this), "Tab3", intent);
    }

    private void setupTab(final View view, final String tag, Intent intent) {
        View tabview = createTabView(tabHost.getContext(), tag);
        TabHost.TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabview).setContent(intent);
        tabHost.addTab(setContent);
    }

    private static View createTabView(final Context context, final String text) {
        View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null); 
        TextView tv = (TextView) view.findViewById(R.id.tabsText);
        tv.setText(text);
        return view;
    }
}

选项卡上的一个活动(MyTab2)注册一个BroadcastReceiver,以便在特定事件上,选项卡将刷新其显示的数据。我没有在onPause中取消注册BroadcastReceiver,因为我希望这个活动刷新自己,即使它当前不在前面。

public class MyTab2 extends Activity {

    // listener to receive broadcasts from activities that change
    // data that this activity needs to refresh on the view
    protected class MyListener extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("myapp.REFLECT__CHANGES")) {
                //request new data from the server
                refreshData();
            }
        }
    }

    protected void onResume() {
        // if listener is not registered yet, register it
        // normally we would unregister the listener onPause, but we want
        // the listener to fire even if the activity is not in front
        if (!listenerIsRegistered) {
            registerReceiver(listener, new IntentFilter("myapp.REFLECT_CHANGES"));
            listenerIsRegistered = true;
        }
        super.onResume();
    }

public class MyTab2 extends Activity { // listener to receive broadcasts from activities that change // data that this activity needs to refresh on the view protected class MyListener extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("myapp.REFLECT__CHANGES")) { //request new data from the server refreshData(); } } } protected void onResume() { // if listener is not registered yet, register it // normally we would unregister the listener onPause, but we want // the listener to fire even if the activity is not in front if (!listenerIsRegistered) { registerReceiver(listener, new IntentFilter("myapp.REFLECT_CHANGES")); listenerIsRegistered = true; } super.onResume(); }

我的问题是,我发现即使在备份到MyTabActivity显示之前的活动并再次启动MyTabActivity之后,在第一次进入MyTabActivity时创建的MyTab2实例似乎仍然活着当我广播触发选项卡刷新其数据的事件时,MyTab2的旧实例和MyTab2的新实例都会刷新,因为它们都接收广播。 我的问题是,当退出TabActivity时,如何杀死TabActivity的所有子活动?

儿童活动的旧实例是否存在,因为当活动离开前线时,我没有取消注册BroadcastReceiver?

您可以想象,每次启动后续MyTabActivity时,此问题都会加剧。

1 个答案:

答案 0 :(得分:1)

无需播放此内容:

实施此操作:

Intent intent = new Intent().setClass(this, MyTab1.class);
        setupTab(new TextView(this), "Tab1", intent);
        intent = new Intent().setClass(this, MyTab2.class);
        setupTab(new TextView(this), "Tab2", intent);
        intent = new Intent().setClass(this, MyTab3.class);
        setupTab(new TextView(this), "Tab3", intent);

就这样做

 tabHost = (TabHost)findViewById(android.R.id.tabhost);
        tabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
        Intent intent = new Intent().setClass(this, MyTab1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        setupTab(new TextView(this), "Tab1", intent);
        intent = new Intent().setClass(this, MyTab2.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        setupTab(new TextView(this), "Tab2", intent);
        intent = new Intent().setClass(this, MyTab3.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        setupTab(new TextView(this), "Tab3", intent);

多数民众赞成,你的问题将得到解决