10 Temmuz 2012, Salı
Nasıl geçerli ön plan etkinlik kapsamında android için?
Benim yayın yürütülür zaman ön planda etkinlik için Uyarı göstermek istiyorum.
CEVAP
21 Aralık 2012, Cuma
(Not:Resmi bir API API 14 eklendi: bu cevap http://stackoverflow.com/a/29786451/119733)
ÖNCEKİ (waqas716) cevap kullanmayın.
Bellek sızıntısı sorunu, faaliyet için statik bir başvuru nedeniyle olacak. Daha fazla ayrıntı için aşağıdaki bağlantıyı http://android-developers.blogspot.fr/2009/01/avoiding-memory-leaks.html bkz
Bunu önlemek için, faaliyetler, referanslar yönetmek gerekir. Dosya listesi: uygulama adını ekleyin
<application
android:name=".MyApp"
....
</application>
Uygulama sınıfı :
public class MyApp extends Application {
public void onCreate() {
super.onCreate();
}
private Activity mCurrentActivity = null;
public Activity getCurrentActivity(){
return mCurrentActivity;
}
public void setCurrentActivity(Activity mCurrentActivity){
this.mCurrentActivity = mCurrentActivity;
}
}
Yeni bir Etkinlik oluşturun :
public class MyBaseActivity extends Activity {
protected MyApp mMyApp;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMyApp = (MyApp)this.getApplicationContext();
}
protected void onResume() {
super.onResume();
mMyApp.setCurrentActivity(this);
}
protected void onPause() {
clearReferences();
super.onPause();
}
protected void onDestroy() {
clearReferences();
super.onDestroy();
}
private void clearReferences(){
Activity currActivity = mMyApp.getCurrentActivity();
if (currActivity != null && currActivity.equals(this))
mMyApp.setCurrentActivity(null);
}
}
Şimdi faaliyetleri için Faaliyet sınıfı genişletme yerine, sadece MyBaseActivity uzatın. Şimdi, uygulama veya Faaliyet bulunduğunuz aktivite gibi içerik alabilir :
Activity currentActivity = ((MyApp)context.getApplicationContext()).getCurrentActivity();
Bunu Paylaş:
Nasıl Android Şeffaf Etkinlik oluşturm...
Nasıl bir arka plan android %20 şeffaf...
Nasıl arka plan rengini değiştirmek iç...
&Quot Geçersiz kılmak İçin Nasıl Andro...
Nasıl bir telefon görüşmesi bittiğinde...