-1

I am using an event listener in my main activity to determine network connectivity with the database

//Main Activity

    new MyListener() {
        if (connected) {
            Log.d(TAG,"connected");
            Snackbar.make(_current_view_,"Connected to Firebase", Snackbar.LENGTH_SHORT).show();
       }
       else {
            Log.d(TAG,"disconnected");
            Snackbar.make(__current_view_, "Disconnected from Firebase", Snackbar.LENGTH_SHORT).show();
        }       
    });

Now, since this is a listener in the main activity, every time network connectivity changes, this gets elegantly called. But I need a view of the current activity being rendered by the Application.

How do I get the current view? The listener works and the logging is perfect but the Snackbar is not called, because we are not on the layout of the main activity. When we are though, the snackbar gets displayed. Is it possible to get the reference of current view?

When I replace SnackBars with Toasts, they work in tandem with the log perfectly.

Toast.makeText(Login.this, "Connected to Firebase", Toast.LENGTH_SHORT).show(); //Works

I tried:

getCurrentFocus()
findViewbyId(android.R.id.content)
getWindow().getDecorView()
getWindow().getDecorView().getRootView()

None, of which works. Any help would be appreciated.

isopropylcyanide
  • 423
  • 4
  • 16
  • Can you give more information about your architecture ? is MainActivity the host to Fragments ? What do you mean about the Current View ? If Activity is not in foreground how do you get MyListener's methods work ? – osayilgan Dec 11 '15 at 22:48
  • Good point. The listener is a firebase database listener, which once registered stays up until explicitly removed. It works seamlessly across activities. When I used Toasts, it worked just fine. It's just the main activity, no host to any fragment. – isopropylcyanide Dec 11 '15 at 22:50

1 Answers1

0

So After your comment, What I understood is, not the expert in Firebase though, You should probably register your listener in onResume and unregister it in onPause. So you won't get any conflict there.

But regarding to solution, you need to register all of your activities in the same way. So if you navigate from MainActivity to Activity-A, then in onPause of MainActivity, unregister your listener. And in the Activity-A, register your database listener in onResume of the that new Activity. So you will get the Current Activity and the Current View. Your listener will call the most recent Activity on stack, instead of keeping MainActivity's instance up for all time. Which can cause other problems.

If the above solution is not good for you, and you only need the Current View, why don't you save the Current View every time you change the top activity in stack. You can have a View Object in the Application class, where you can reach statically. You can Just create static getter and setter methods in your Applications Class. But I'm still not sure if this would be a better way. Keeping a static View instance in the Application Class.

osayilgan
  • 5,873
  • 7
  • 47
  • 68
  • I wish I could explain, but registering and unregistering a network listener so many times is inefficient, considering that the functionality of SnackBar can be replaced by a Toast. There has to be an alternative – isopropylcyanide Dec 11 '15 at 23:16
  • Well, there are alternatives, of course. But I wanted to do it in a better way. see my edited answer. – osayilgan Dec 11 '15 at 23:18