Detect current foreground Activity and reload it

337 views Asked by At

My Problem: In MainActivity I start to download some data from the Internet via XML Parser. This data is displayed on every Screen of my App. It is like a Database.

While downloading and saving data, the user of the App can navigate through the hole app. If downloading and saving data is finished, I want to detect and reload the current foreground Activity. I need to do this, to display the new data.

I can detect the current Activity with the ActivityManager and can start it with the ComponentName with the method "startActivity(Intent.makeMainActivity(componentName)". But then the old activity is also there when I navigate back with the ActionBar-BackButton. I do this in the AsyncTasks onPostExecute-Method after downloading Data.

Any Ideas how to do it?

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

There are 1 answers

6
Tomasz Czura On BEST ANSWER
  1. Download your data is service, not in AsyncTask
  2. Register some BroadcastReceiver in your MainActivity in onStart(), and unregister it in onStop(). Refresh your view in this receiver
  3. When data are downloaded, make a broadcast to call activity, that data are ready. Disadvantage of this method is that you should save your data somewhere. You can of course pass data in the intent, but only if data are small