2012-11-01 30 views
5

Tôi có một TabActivity gọi MyTabActivity chứa nhiều tab mà tôi tạo ra sử dụng mã này:TabActivity con Hoạt động với BroadcastReceiver vẫn còn sống trong nền

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

Một trong những hoạt động trên một tab thanh ghi (MyTab2) một BroadcastReceiver để trên một sự kiện cụ thể, tab sẽ làm mới dữ liệu được hiển thị của nó. Tôi không hủy đăng ký BroadcastReceiver ở chế độ onPause, vì tôi muốn hoạt động này tự làm mới ngay cả khi nó hiện không ở phía trước.

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

Vấn đề của tôi là tôi thấy rằng ngay cả sau khi sao lưu vào các hoạt động mà trước màn hình hiển thị của MyTabActivity và bắt đầu MyTabActivity lần thứ hai, mà trường hợp của MyTab2 đã được tạo ra trên lối vào đầu tiên vào MyTabActivity dường như vẫn còn sống và khi tôi phát sự kiện kích hoạt tab để làm mới dữ liệu của nó, cả phiên bản cũ của MyTab2 và phiên bản mới của MyTab2 đều làm mới vì cả hai đều nhận được phát sóng. Câu hỏi của tôi là làm thế nào tôi có thể giết tất cả các hoạt động của trẻ trong TabActivity khi sao lưu ra khỏi TabActivity?

Trường hợp cũ của các hoạt động trẻ em có ở xung quanh không vì tôi không đăng ký BroadcastReceiver khi hoạt động rời khỏi mặt trận?

Như bạn có thể hình dung, vấn đề này được kết hợp mỗi khi một MyTabActivity tiếp theo được bắt đầu.

Trả lời

1

Không cần phải phát sóng này:

Instaed để làm điều này:

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

chỉ làm điều này

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

Thats nó, và vấn đề của bạn sẽ được đã giải quyết

+0

Intent.FLAG_ACTIVITY_CLEAR_TOP, tuyên bố này xóa hoạt động hàng đầu được thêm vào Ngăn xếp. và sau đó onCreate() được gọi. Điều này xảy ra trong trường hợp của tôi. –

Các vấn đề liên quan