我有以下几点:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
               ....
               populate();
                handler = new Handler();
        t = new Timer();
        t.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        doReload1();
                        populate();
                    }
                });

            }

        }, 300, 30000);
}

private void populate() {
    if (playlists.length != 0) {
        MyListView = (ListView) findViewById(R.id.MyListView);
        for (String item : playlists) {
            Log.d("DEBUG", "item=" + item);
        }

        String[] adapterPlaylists = new String[playlists.length];
        for (int i = 0; i < playlists.length; i++) {
            adapterPlaylists[i] = playlists[i];
        }
        adapter1 = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, adapterPlaylists);

        MyListView.setAdapter(adapter1);
        adapter1.notifyDataSetChanged();

        MyListView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View v,
                    int position, long id) {
                dialogwait = ProgressDialog.show(Playlist.this,
                        "Loading...", "Please wait..", true);
                Intent i = new Intent(getBaseContext(), ViewPlaylist.class);
                i.putExtra("id", idPlaylist[position]);
                i.putExtra("timer", timerPlaylist[position]);
                startActivity(i);

                finish();
            }
        });
    } else
        System.out.println("playlist null");

}

@Override
protected void onPause() {
    super.onPause();
    System.out.println("onPause Playlist!!!!!!");
    dialogwait.dismiss();
    t.cancel();
}


问题是这里:

dialogwait = ProgressDialog.show(Playlist.this,
                            "Loading...", "Please wait..", true);


我创建一个ProgressDialog,然后在onPause()中分发它。
但是在我甚至创建onPause之前,onCreate()就在ProgressDialog之后被调用。
知道为什么吗?有解决方案吗?

最佳答案

这是因为Android中的Dialog不会阻塞-意味着在它后面运行的线程(在这种情况下,您的Activity尤其是您的onItemClickListener)将继续执行。

您似乎想显示一个加载对话框,以使用户知道他单击的项目正在加载。我建议您将该对话框移至下一个活动(该活动从onClick开始),然后从那里显示并关闭该对话框。

关于android - 在离开当前 Activity 之前调用onPause,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8062427/

10-11 15:01