0

How could I know if an activity is the top of stack? I thought about using onResume/onPause, but this is not exactly, as it would fail once the app goes to background. The fact is that I'm sending a broadcast receiver that is received for all activities (I have a BaseActivity that is extended by all activities and that registers to the broadcast). So, only the activity that is at the top of the stack must react to the broadcast. If I use the isResumed() then it would work always but when the app goes to background. Any idea?

Thanks in advance!

FVod
  • 2,245
  • 5
  • 25
  • 52
  • 1
    http://stackoverflow.com/questions/3262157/how-to-check-if-my-activity-is-the-current-activity-running-in-the-screen – sasikumar Feb 26 '16 at 11:51
  • Thank you for your answer, but this is exactly what I said I'm doing. The problem is that this solution is not totally right, if the app goes to background then no activity is resumed, so no activity handles the broadcast receiver. – FVod Feb 26 '16 at 11:56

1 Answers1

0
        in base activity you register the broadcast Receiver and in receiver function you call one abstract function which one is implemented by all child activities.
        The  activity which is on top will automatically receive that function call.
        Edit sample code:

        public abstract class BaseActivity extends AppCompatActivity {
            private static final String NOTIFICATION_ARRIVED = "arrived";
            public abstract void receivedFunction(Intent intent);
            private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    displayToast(" received in Base");
                    receivedFunction(intent);
                }
            };

            public void displayToast(String s) {
                Toast.makeText(this,s,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResume() {
                super.onResume();
                registerReceiver(mMessageReceiver, new IntentFilter(BaseActivity.NOTIFICATION_ARRIVED));
            }

            @Override
            public void onPause() {
                super.onPause();
                unregisterReceiver(mMessageReceiver);
            }
        }

        public class MainActivity extends BaseActivity {
         @Override
            public void receivedFunction(Intent intent) {
                displayToast(" received in child");
            }
        // do whetever you want . if you ovveride onpause and onResume then call super as well
        }
    or any other child

     public class MainActivity2 extends BaseActivity {
         @Override
            public void receivedFunction(Intent intent) {
                displayToast(" received in child");
            }
        // do whetever you want . if you ovveride onpause and onResume then call super as well
        }

// to broadcast

Intent intent = new Intent(BaseActivity.NOTIFICATION_ARRIVED);
        sendBroadcast(intent);
Khizar Hayat
  • 3,427
  • 3
  • 18
  • 22
  • All created activities receive the broadcast Receiver, as the register is done onCreate and unregistered on onDestroy. But I need to detect which is the top acitivty so only this one handles the receiver. I'm solving that issue by using the onResume/onPause method but, as I said, this is not totally right as when the app goes to background then no activity handles the receiver. – FVod Feb 26 '16 at 11:59
  • register in oncreate of baseActivity and unregister in ondestroy or onpause in baseActivity. wait i am sending you sample hope that will help you – Khizar Hayat Feb 26 '16 at 12:00
  • Thank you very much for the example, the problem is that when the app goes to background, the activity is paused, so the broadcast receiver is never received – FVod Feb 26 '16 at 12:20
  • what about service ? service is run on background – Khizar Hayat Feb 26 '16 at 12:23