TabActivity的子Activity和BroadcastReceiver在后台保持活动状态

5

我有一个名为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)上的一个活动注册了一个广播接收器,这样在特定事件发生时,该标签页将刷新其显示的数据。我没有在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();
    }

我的问题是,即使我回退到显示MyTabActivity之前的活动并第二次启动MyTabActivity,似乎在第一次进入MyTabActivity时创建的MyTab2实例仍然存在,并且当我广播触发选项卡刷新其数据时,旧的MyTab2实例和新的MyTab2实例都会刷新,我的问题是如何在回退TabActivity时杀死所有子活动?
旧的子活动实例是否仍然存在是因为我没有在活动离开前注销BroadcastReceiver吗?
正如你所想象的那样,每次启动后续的MyTabActivity时,这个问题就会变得更加复杂。
1个回答

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

就是这样,你的问题就会得到解决。


Intent.FLAG_ACTIVITY_CLEAR_TOP,此语句清除添加到堆栈中的顶部活动。之后将调用onCreate()。这在我的情况下发生了。 - Narendra Pal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接