1
votes

I want to launch an activity, and show the progress of the loading of the activity as methods are completed. The methods will, for example, be downloading data. The problem is, the loading bar never actually shows as empty at the beginning, it simply loads once all methods are complete and shows 100% full.

I have tested a few implementations, but with no luck. The current state I am in is that I set the loading bar to max in onCreate, then carry out processing in onWindowFocusChanged. The issue is, that the loading bar is never drawn after onCreate. I have tested placing methods into onResume also, but again the progress bar only appears AFTER onCreate, onResume and onWindowFocusChanged have all completed. This has thrown my understanding of the activity lifecycle off a bit as I thought all visual elements should be drawn after onCreate?

My current code is as follows (note that no difference is noticed if the code in onWindowFocusChanged is placed in onResume). This code is all prototyping code in an attempt to get this to work.

public class IntermediateLoadingScreen extends AppCompatActivity {

    private ProgressBar loadingBar = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intermediate_loading_screen);
        loadingBar = (ProgressBar)findViewById(R.id.intermediateLoadingBar);
        loadingBar.setMax(100);
        Log.d("IntermediateLoadScreen","onCreate Complete");    
    }

    @Override
    public void onResume () {
        super.onResume();
        Log.d("IntermediateLoadScreen","onResume");
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        Log.d("IntermediateLoadScreen","onWindowFocusChanged");
        if(hasFocus){
            AsyncCollectDeviceData asyncCollectDeviceData = new AsyncCollectDeviceData(this.getApplicationContext());
            asyncCollectDeviceData.execute();
            loadingBar.setProgress(15);
            Log.d("IntermediateLoadScreen", "Loading");
            getUserContactsFromServer();
            getUserGroupsFromServer();
            loadingBar.setProgress(100);
            /*Intent intent = new Intent(this, nextActivity.class);
            startActivity(intent);*/
        }
    }

    public void getUserContactsFromServer(){
        Log.d("IntermediateLoadScreen", "getUserContactsFromServer");
        loadingBar.setProgress(35);
        ContactsResponse contactsResponse = new ServerRequest().submitContactsRequest();
        new UserRealmDAO().updateContacts(contactsResponse);
        loadingBar.setProgress(60);
    }

    public void getUserGroupsFromServer(){
        Log.d("IntermediateLoadScreen", "getUserGroupsFromServer");
        loadingBar.setProgress(80);
        GroupsResponse groupsResponse = new ServerRequest().submitGroupsRequest();
        new UserRealmDAO().updateGroups(groupsResponse);
        loadingBar.setProgress(95);
    }
}

I understand that I shouldn't really have a loading screen in an activity of it's own, and should probably have it as part of an activity that I can make visible when required. But the same activity flow should occur, so if I can get this example working it should transfer across.

To conclude, the loading bar does not show after onCreate, the previous activity simply hangs and a complete loading bar eventually pops up. How can I have the empty loading bar drawn at the beginning and fill it as i call 'loadingBar.setProgress'?

EDIT: These are my logs I have added to the above activity, I have moved the code shown in onWindowFocusChanged into onResume:

01-20 13:10:15.430 /IntermediateLoadScreen: onCreate Complete
01-20 13:10:15.430 /IntermediateLoadScreen: onResume
01-20 13:10:20.425 /IntermediateLoadScreen: Bar set at progress 15
01-20 13:10:26.242 /IntermediateLoadScreen: Bar set at progress 80
01-20 13:10:31.457 /IntermediateLoadScreen: Bar set at progress 100

The first (splashscreen) activity stays displayed until all these logs have printed out. I have placed a Thread.sleep before every progress bar set call, so you can see that there is a at least 5 seconds between each log. Even after doing this, my activity does not display until ALL method calls in the entire class are complete.

2
Just Curious. What are you trying to achieve ? Are you trying to do some heavy duty task and update the progress bar once it is done ? Any UI update on the UI thread. And from your code it looks like you are doing all the heavy duty task on the UI thread which might interfere with the UI update.Dibzmania
Try the simple work pattern shown in the Progress bar dev page - developer.android.com/reference/android/widget/ProgressBar.html Offload all work to a secondary thread.Dibzmania
Ok thanks, I'll read over that link!Calco

2 Answers

1
votes

Problem is onWindowFocusChanged(boolean hasFocus) will be called right after activity is created. So your progress bar will have 15% at beginning. Also all your methods may run really fast, so they will be done before user can actually see some progress

1
votes

Just a suggestion here. Generally if any data is to be loaded/background process to take place, there is a new feature in Android Lollipop called SplASHsCREEN. Try using THIS Splash screen- a basic app screen where it will be shown till you load all your data and after it loads, you can show the next screen.

This gives user a good feel just after launching the app instead of displaying progress bar.