1

I have a simple Android app - a dictionary. I am using a ListView to populate the dictionary words on the main glossary screen. If a word is touched a new intent launches a wordscreen that shows the word and the definition. The problem is that if I hit "back" to go from the wordscreen back to the glossary the ListView does not populate.

I am using onStop() to close the database cursor when moving from the glossary screen, and onRestart() to open the cursor and (I thought) to populate the ListView. It doesn't seem to be working.

One important thing, if after going back to the glossary screen from the word screen (and getting a black screen with no ListView) I change the orientation of the app the ListView populates.

I thought that pressing "back" would always cause onRestart() to execute. Why would pressing back NOT cause the ListView to populate, but changing the orientation does?

Thanks

user548956
  • 136
  • 4

2 Answers2

3

Use onPause and onResume instead. Changing orientation causes your activity to be destroyed and restarted, navigating to another activity causes your activity to be paused. When you click back it's resumed.

Check this out for an overview - http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

Ryan Reeves
  • 10,209
  • 3
  • 42
  • 26
  • Ok, thanks. I had read over the Activity Lifecycle documentation - I think I may have reversed the visible lifetime and foreground lifetime. – user548956 Dec 20 '10 at 18:16
0

Have the same issue, but mine could be a little different from yours.

This is what worked for me, in the main activity (the one that has the ListView that does not repopulate on back), I put this:

@Override
    protected void onRestart() {
        Intent intent = getIntent();
        finish();
        startActivity(intent);
        super.onRestart();
    }

Probably a hack and inefficient, but works for my small app!

Edit: Here's a better method that worked with me too:

@Override
    protected void onRestart() {
        this.recreate();
        super.onRestart();
    }

Note: I think the reason why this is happening to my app is because I start the new activity from a dialogue box (and had to set the flag Intent.FLAG_ACTIVITY_NEW_TASK). But I'm still learning and could be mistaken.

Rani Kheir
  • 1,049
  • 12
  • 15