Is it any way to check Activity lifecycle state outside an Activity? It is easy to achieve maybe, but I can't find it anywhere.
Asked
Active
Viewed 581 times
1
-
what you want to do ? – Vishal Patoliya ツ Sep 01 '16 at 09:24
-
Store a reference in application level. – Sunil Sunny Sep 01 '16 at 09:26
-
http://stackoverflow.com/a/14470360/5545429 see this – shinilms Sep 01 '16 at 09:29
-
@ShinilMS your answer helped me, thank you. I haven't thought about callbacks... – Igor Mańka Sep 01 '16 at 09:36
-
Happy to help! :) – shinilms Sep 01 '16 at 09:41
2 Answers
0
You can put a static method and variable in the Activity, then in the onPause
, onCreate
, onResume
you can modify the variable. From other activities you can call the variable to know the activity state.
Activity1:
public static int state = 0;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
Activity1.state = 1;
...
}
@Override
protected void onPause()
...
Activity1.state = 2;
...
}
@Override
protected void onResume() {
...
Activity1.state = 3;
...
}
Then you can check for the state in other activities like:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
int state = Activity1.state;
Toast.makeText(this,"Activity1 state is "+state, Toast.LENGTH_LONG).show();
...
}
...Hope it helps

Collins
- 145
- 2
- 15
-
Yes, it's a kind of solution, but I want to avoid static fields :D Thank you. – Igor Mańka Sep 01 '16 at 09:56
0
You can store the activity state in Shared preference and use it whenever and wherever you want.

raasesh
- 161
- 11