1

I want to access the activity lifecycle method of a different activity from the present one... Can i do that? for example i have 2 activities A and B. I want to access the onStop method of activity A from activity B. can i do that? i'm trying to check the online of a user in my app which has multiple activities so i want to write code which is like = If onStop/onDestroy method of both the activities are called show that the user is offline... The code im using is

        @Override
    public void onStart(){
    super.onStart();
    mDatabaseReference.child("Online").setValue(true);
    }

        @Override
    public void onStop(){
    super.onStop();
    mDatabaseReference.child("Online").setValue(false);
}

Can someone please help me out

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • You probably can use some ugly workaround to do that, but Android Architecture is designed the way that you should not access one activity from other. Correct way will be to refactor your app's arcitecture – Vladyslav Matviienko Jul 09 '18 at 07:02
  • Or you can use callbacks. – Ranjan Jul 09 '18 at 07:03
  • You can create separate singleton class and save the state of activities there. Then you can access the state of the saved states of activities in other activities. – MarcinR Jul 09 '18 at 07:03
  • You cannot do that, If you want to check the availability of the user there are other ways to do it. – Umair Jul 09 '18 at 07:04
  • can i know what the other ways are @Umair How do i achieve this using callbacks –  Jul 09 '18 at 07:05
  • @EmiliaJames easy way is to put a flag in your logic. So if any user goes offline you send the flag to server using web service. Take a look at this question for further assistance. https://stackoverflow.com/questions/35947316/how-to-know-if-app-user-is-offline And other way is to use broadcast receivers. You can use callbacks too if you can tell me what type of callbacks you want to use that will help :) – Umair Jul 09 '18 at 07:08
  • Can i get the complete code of it please and where to type it? because i have never worked with these and have no clue about its working @Umair –  Jul 09 '18 at 07:13
  • I am sure you will find relevant code on the internet. If you can the link I posted in comment above you will know where you can type it. As far as broadcast receivers are concerned first see how do we use receivers and for call back you got the answer below follow the links and you will be good. – Umair Jul 09 '18 at 07:16
  • You can simply use application class for your requirement – Quick learner Jul 09 '18 at 07:18
  • Its better to use sharedPreferences to set the value. – Ankita Jul 09 '18 at 10:52

2 Answers2

0

Use Application.ActivityLifecycleCallbacks in your Application class. This way you just need to register your activities to the callbacks and from the application class only you can track down wheather any activity is present or not.

For more info please refer to this answer

Brijesh Joshi
  • 1,817
  • 1
  • 9
  • 24
0

To set the value You can use SharedPreferences. Declare the instance of sharedpreference at application level.

In Activity A and B you can set the value in onStop(), onDestroy() and onStart() block.

Ankita
  • 1,129
  • 1
  • 8
  • 15
  • can i get the code on how to achieve that please... i have never worked on sharedpreferences before for this purpose –  Jul 10 '18 at 13:13