0

I want to make a database request whenever the main activity is dislpayed on the screen , the main activity is the one defined in the manifest file :

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

if I start another activity by clicking a menu item for example , then when I finish it by pressing the back button or the code executes a finish command then I want the database request to be launched because at this moment it is the main activity which is displayed. So how to know that the main activity is displayed ?

pheromix
  • 18,213
  • 29
  • 88
  • 158

2 Answers2

1

Look at the activity Life Circle, You shoud put your code into onResume(). Hope it can help :) Image for Activiy life circle Activity Life circle

ThaiPD
  • 3,503
  • 3
  • 30
  • 48
1

Using a Boolean in onResume(), and onPause() could get you your desired effect. Once the activity is resumed, your code in onResume() would change the Boolean from FALSE to TRUE, which then could activate a Listener you have set up on that Boolean.

EDIT: You also have to set code in onPause() to change the Boolean from TRUE to FALSE.

For some related questions that have been answered, here you go: Check whether activity is active and Android: how do I check if activity is running?

Community
  • 1
  • 1
Steven_BDawg
  • 796
  • 6
  • 21