Update View at runtime in Android

前端 未结 3 1143
傲寒
傲寒 2021-01-13 08:53

The example is pretty straightforward: i want to let the user know about what the app is doing by just showing a text (canvas.drawText()). Then, my first message appears, bu

相关标签:
3条回答
  • 2021-01-13 09:36

    First: onCreate is executed on main UI thread of application so no UI updates until you leave it. Basically you need one thread to execute long running tasks and some mechanism to push updates into the UI.
    Most usual approach is to extend AsyncTask see this link for further info

    0 讨论(0)
  • 2021-01-13 09:37

    i suppose that your view is an extended view and you call onDraw for drawing the view, so, maybe the view isn´t 'refresh' their state, so try this

    onCreate(Bundle bundle) {
        setContentView(splash); // splash is the view class
    
        loadResources();
        splash.setText("this");
        splash.invalidate();
        boundWebService();
        splash.setText("that"):
        splash.invalidate();
        etc();
        splash.setText("so on");
        splash.invalidate();
    }
    
    0 讨论(0)
  • 2021-01-13 09:38

    I haven't hit this yet, but I think the usual pattern is to do lengthy initialization in a background thread, and use Handler.post() to update the UI. See http://developer.android.com/reference/android/widget/ProgressBar.html for a different, but possibly related, example.

    Also see this answer, especially the first paragraph:

    The problem is most likely that you are running the splash screen (some sort of Dialog such as ProgressDialog I assume) in the same thread as all the work being done. This will keep the view of the splash screen from being updated, which can keep it from even getting displayed to the screen. You need to display the splash screen, kick off an instance of AsyncTask to go download all your data, then hide the splash screen once the task is complete.

    Update (based on your update and your comment): You are not supposed to update the UI in any thread except the one where your Activity is created. Why is it impossible for you to load your resources in a background thread?

    0 讨论(0)
提交回复
热议问题