2012-11-01 7 views
5

Ho un TabActivity chiamato MyTabActivity contenente diverse schede che creo utilizzando questo codice:TabActivity attività bambino con BroadcastReceiver rimane vivo in background

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

Una delle attività in una scheda (MyTab2) registri un BroadcastReceiver in modo che su un evento specifico, la scheda aggiorni i suoi dati visualizzati. Non sto annullando la registrazione di BroadcastReceiver in onPause, perché voglio che questa attività si aggiorni automaticamente anche se non è attualmente in primo piano.

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

mio problema è che mi sto trovando che anche dopo il backup su l'attività che ha preceduto la visualizzazione di MyTabActivity e iniziare MyTabActivity una seconda volta, che l'istanza di MyTab2 che è stato creato il primo ingresso in MyTabActivity sembra essere ancora vivo e quando trasmetto l'evento che attiva la scheda per aggiornare i suoi dati, sia la vecchia istanza di MyTab2 che la nuova istanza di MyTab2 si stanno rinnovando perché entrambi ricevono la trasmissione. La mia domanda è: come posso uccidere tutte le attività figlio di TabActivity quando si esce da TabActivity?

I vecchi casi di attività figlio restano in giro perché non annullo la registrazione di BroadcastReceiver quando l'attività viene abbandonata?

Come potete immaginare, questo problema viene aggravato ogni volta che viene avviata una successiva MyTabActivity.

risposta

1

Non c'è bisogno di trasmettere questo:

instaed di fare questo:

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

solo fare questo

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

Ecco fatto, e il problema sarà risolto

+0

Intent.FLAG_ACTIVITY_CLEAR_TOP, questa istruzione cancella l'attività principale che viene aggiunta allo Stack. e successivamente viene chiamato onCreate(). Questo è successo nel mio caso. –

Problemi correlati