0
我的問題: 在MainActivity中,我開始通過XML Parser從Internet下載一些數據。該數據顯示在我的應用程序的每個屏幕上。它就像一個數據庫。檢測當前前臺活動並重新加載它
在下載和保存數據時,應用程序的用戶可以瀏覽洞應用程序。如果下載和保存數據完成,我想檢測並重新加載當前的前景活動。我需要這樣做,以顯示新的數據。
我可以使用ActivityManager檢測當前活動,並可以使用ComponentName和startActivity(Intent.makeMainActivity(componentName))方法來啓動它,但是當我使用ActionBar-返回按鈕。 我這樣做的AsyncTasks onPostExecute法下載數據後。
任何想法怎麼辦呢?
protected void onPostExecute(MainActivity.UrlParams result) {
Toast.makeText(result.contextMain, "Daten wurden erfolgreich aktualisiert!", Toast.LENGTH_LONG).show();
//Aktualisiert aktive Activity damit die neuen Daten angezeigt werden!
ActivityManager am = (ActivityManager) result.contextMain.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
Log.d("topActivity", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName());
ComponentName componentInfo = taskInfo.get(0).topActivity;
componentInfo.getPackageName();
Intent intent = Intent.makeMainActivity(componentInfo);
result.contextMain.startActivity(intent);
//myApp.getCurrentActivity().set
}
有一個名爲'''EventBus''的庫,可以爲你做到這一點,它很棒。如果你不想使用這個庫,托馬斯建議的是最好的方法。 – danypata
謝謝,但有一個簡單的方法來做到這一點? – Goetti
隨着danypata提到,你可以使用'EventBus'庫。我認爲這是最簡單,最好的方法,沒有任何黑客和模糊代碼 –