简体   繁体   中英

onPause gets called before leaving the current activity

I have the following:

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();
}

The thing is that here:

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

I create a ProgressDialog and I dissmis it in onPause() . But onPause gets called right after onCreate() before I even the ProgressDialog is created. Any idea why?ANy solution?Thanks

This is because a Dialog in Android, does not block - meaning that the thread running behind it (in this case your Activity and in particular your onItemClickListener ) will continue to execute.

It looks like you want to display a loading dialog, to let the user know that the item he clicked is being loaded. I suggest you move that dialog to the next activity (the one started from onClick), and then display and dismiss the dialog from there.

I think that the problem could be that you are calling finish() in your OnItemClickListener . This will cause the current Activity to end as soon as you click on an item which, because of your onPause() implementation will immediately close the dialog.

Maybe you need to display the dialog when your ViewPlaylist Activity is created rather than in this Activity.

anything on top of your current activity (a dialog or any other activity) makes it a background activity, thus onPause is called. if anything, dialog.dismiss() should be called in activities' onResume. in your implementation, you're showing a dialog when a button is clicked, thus pausing your activity and calling dismiss. you should call dismiss only when the process associated with your dialog is finished, thus telling your user that you're ready to do something else - in your case launch another activity. launch your activity inside your dialog's onDismiss. for example:

AlertDialog alert = new AlertDialog.Builder(this).create();
  alert.setOnDismissListener(new OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {
      // start the other activity
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM