Android的 - 不能申请修饰模式的活动?模式、Android

2023-09-12 05:59:48 作者:旺仔尐饅頭

我希望能够动态地构建有,我们有(如prevent滚动功能的一些的活动,监视互联网连接,prevent方向变化等)。

我们不希望有创建一个抽象的 BaseActivity 包含所有功能的所有可能的属性和抽象方法。我们会有很多空函数实现的 BaseActivity 的子类,谁只需要​​实际执行数相同的功能。更糟糕的是,如果我们想到另一个功能(另一套性能和功能添加到 BaseActivity ),我们将不得不修改的每一个孩子 BaseActivity 实施新的抽象功能(最有可能会是空的)。

BaseActivity 继承的另一种方法是创建一个实现只有少数的,我们有能力子类。但是,这需要我们创建一个子类的功能,每个组合,这实在是太多了,甚至有一些能力。另外,这会导致大量的重复code为2+的子类,将实现同样的功能。

所以,我想实现装饰图案的,但我不认为我们的会被实例化一个新的活动。我们始终创建一个意图并指定 SomeActivity.class 在其构造,则称 startActivity (新意图(getApplicationContext(),SomeActivity.class));

有没有办法拦截一个活动的实际实例,就可以进行修饰模式操作,然后让OS /应用程序把它放在堆栈的顶部?

如果没有,还有什么其他的方法是可行的?

解决方案   Android动态申请权限

我们不希望有创建一个抽象BaseActivity,其中包含所有功能的所有可能的属性和抽象方法。

您可以通过删除关键字摘要做到这一点。那每个关键字需要大约8个按键。

  

有没有办法拦截一个活动的实际实例,就可以进行修饰模式操作,然后让OS /应用程序把它放在堆栈的顶部?

只有通过派生的Andr​​oid。

  

如果没有,还有什么其他的方法是可行的?

创建一个非抽象 BaseActivity 的包含所有可能的属性,对所有功能的方法和非抽象的。覆盖需要在子类中的方法。当它是有道理的,有子链超类,所以超类可以有默认行为的子类可以增加。

您可以看到这一点活动本身。您不必执行的onCreate() ONSTART() onResume()的onPause()的onStop()的onDestroy() 的onSaveInstanceState() onRestoreInstanceState(),和许多其他的方法。这些方法不是抽象的,默认的实现是合理的(与的onCreate的典型异常())。子类可以覆盖在需要添加功能的方法,或在某些情况下,替换现有的功能(例如,的onSaveInstanceState()可以链接到超与否,如需要)。

I would like to be able to dynamically build an Activity that has a few of the capabilities that we have (such as prevent scrolling, monitors internet connectivity, prevent orientation change, etc.).

We don't want to have to create an abstract BaseActivity that contains all possible properties and abstract methods for all capabilities. We would have a lot of empty function implementations in subclasses of BaseActivity who only need to actually implement a few of those functions. Worse yet, if we think of another capability (another set of properties and functions to add to BaseActivity), we would have to edit EVERY child of BaseActivity to implement the new abstract functions (most likely going to be empty).

Another approach of BaseActivity inheritance is to create child classes that implement only a few of the capabilities we have. But this will require us to create a child class for every combination of capabilities, which is too many even with few capabilities. Plus, this would result in lots of duplicate code for 2+ child classes that would implement the same capability.

So, I thought of implementing the Decorator Pattern, but I don't think we ever actually instantiate a new activity. We always create an Intent and specify SomeActivity.class in its constructor, then we call startActivity(new Intent(getApplicationContext(), SomeActivity.class));

Is there a way to intercept the actual instantiation of an Activity, perform Decorator Pattern manipulation on it, and then let the OS/Application put it on top of the stack?

If not, what other approaches are feasible?

解决方案

We don't want to have to create an abstract BaseActivity that contains all possible properties and abstract methods for all capabilities.

You can accomplish this by deleting the keyword abstract. That takes approximately eight keystrokes per keyword.

Is there a way to intercept the actual instantiation of an Activity, perform Decorator Pattern manipulation on it, and then let the OS/Application put it on top of the stack?

Only by forking Android.

If not, what other approaches are feasible?

Create a non-abstract BaseActivity that "contains all possible properties" and non-abstract "methods for all capabilities". Override the methods as needed in subclasses. Where it makes sense, have the subclasses chain to the superclass, so the superclass can have default behavior that the subclass can augment.

You can see this in Activity itself. You do not have to implement onCreate(), onStart(), onResume(), onPause(), onStop(), onDestroy(), onSaveInstanceState(), onRestoreInstanceState(), and many other methods. Those methods are not abstract, and the default implementations are reasonable (with the typical exception of onCreate()). Subclasses can override those methods where desired to add functionality, or in some cases replace the existing functionality (e.g., onSaveInstanceState() can chain to the superclass or not, as desired).