1

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.

Igor Mańka
  • 183
  • 1
  • 1
  • 11

2 Answers2

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
0

You can store the activity state in Shared preference and use it whenever and wherever you want.

raasesh
  • 161
  • 11