【问题标题】:How to know if activity is at the top of stack如何知道活动是否在堆栈顶部
【发布时间】:2016-02-26 11:50:36
【问题描述】:

我怎么知道一个活动是否是栈顶?我考虑过使用 onResume/onPause,但这并不完全正确,因为一旦应用程序进入后台它就会失败。 事实是我正在发送一个所有活动都接收到的广播接收器(我有一个由所有活动扩展的 BaseActivity 并注册到广播)。因此,只有位于堆栈顶部的活动必须对广播做出反应。如果我使用 isResumed() 那么它会一直工作,但是当应用程序进入后台时。有什么想法吗?

提前致谢!

【问题讨论】:

  • 感谢您的回答,但这正是我所说的我正在做的事情。问题是这个解决方案并不完全正确,如果应用程序进入后台,则不会恢复任何活动,因此没有活动处理广播接收器。

标签: android android-activity android-lifecycle android-broadcast android-broadcastreceiver


【解决方案1】:
        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);

【讨论】:

  • 所有创建的活动都会收到广播接收器,因为注册在 onCreate 上完成,在 onDestroy 上取消注册。但我需要检测哪个是最高活动,所以只有这个处理接收器。我正在通过使用 onResume/onPause 方法解决这个问题,但正如我所说,这并不完全正确,因为当应用程序进入后台时,没有任何活动处理接收器。
  • 在 baseActivity 的 oncreate 中注册,在 baseActivity 的 ondestroy 或 onpause 中取消注册。等等我给你寄样品希望对你有帮助
  • 非常感谢你的例子,问题是当应用程序进入后台时,活动被暂停,所以广播接收器永远不会收到
  • 服务怎么样?服务在后台运行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多