简体   繁体   English

网络连接检查的地方

[英]a place for network connection checking

I've got a simple hiearachy of activities where: A is abstract and B,C, etc inherit from A. I need a place to handle network connection and it would be good to place it in the parent activity(A). 我有一个简单的活动层次,其中:A是抽象的,B,C等是从A继承的。我需要一个处理网络连接的地方,最好将其放置在父活动(A)中。 However, I don't see how I can stop code in my child activities(B,C) from executing. 但是,我看不到如何停止子活动(B,C)中的代码执行。 Additionally, in each of the onResume methods of the child activities(B,C) I have extra network code which shouldn't be executed as well. 另外,在子活动(B,C)的每个onResume方法中,我还有一些额外的网络代码,这些代码也不应执行。

So, is there any way apart from "if" in parent onCreate + "if" in child onCreate + "if" in child onResume ? 那么,除了父onCreate的“ if” +子onCreate的“ if” +子onResume的“ if”之外,还有什么办法吗? This approach should be more concise. 这种方法应该更简洁。

keep a boolean in abstract activity A like isNetworkAvliable. 在isactivityAvliable之类的抽象活动A中保留一个布尔值。 check network existence in Oncreate of A or onResume of A ( must call super.onResume in B and C before all work) and put the network in if(isNetworkAvliable). 检查A的Oncreate或A的onResume中的网络存在(必须在B和C中调用super.onResume才能进行所有工作),然后将网络放入if(isNetworkAvliable)中。

Try this... 尝试这个...

  1. Even if B and C are inherited from A, i dont think the methods for handling network will be executed in B and C until you explicitly call them. 即使B和C继承自A,我也不认为在您明确调用它们之前,将在B和C中执行用于处理网络的方法。

  2. And if you want to handle the network connection in class A, then create a method in class A as public void handleConnection() , then make calls from class B's and C's onCreate() and onResume() as super.handleConnection(); 而且,如果要处理类A中的网络连接,则可以在类A中创建一个方法为public void handleConnection() ,然后从类B和类C的onCreate()onResume()进行调用,方法是super.handleConnection();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM