- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在 Android 库中,FragmentActivity延伸Activity .我想添加一些方法,并覆盖原始 Activity 的一些方法.
import android.app.Activity
public class Activiti extends Activity {
public void myNewMethod() { ... }
}
由于原始层次结构,FragmentActivity延伸Activity ,myNewMethod()
也应该出现在我的库的 FragmentActiviti
import android.support.v4.app.FragmentActivity;
public abstract class FragmentActiviti extends FragmentActivity {
public void myNewMethod() { ... }
}
但这会导致重复代码,我不希望这种情况发生。 有没有办法避免这种重复?
Activiti.java
public abstract class Activiti extends Activity {
private int current_orientation = Configuration.ORIENTATION_UNDEFINED; // ORIENTATION_UNDEFINED = 0
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
current_orientation = this.getResources().getConfiguration().orientation;
}
protected boolean isDevicePortrait() { return current_orientation == Configuration.ORIENTATION_PORTRAIT; }
}
FragmentActiviti.java
public abstract class FragmentActiviti extends FragmentActivity {
/* This onCreate() can be omitted. Just putting here explicitly. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
protected void someUtilsForFragments() { /* not used yet */ }
}
E_fragtest_06.java
public class E_fragtest_06 extends FragmentActiviti {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.printf(isDevicePortrait()); // this NOT WORK for now
}
}
我认为使用 Decorator Class将是解决此问题的最好方法(无重复代码)。但是 Decorator Pattern只是有点难以(或不可能)应用于 Android Activity 场景。
我尝试实现@hazzik 的方法,但我仍然遇到一些问题。
ActivityUtil.java
public abstract class ActivityUtil {
private int current_orientation = Configuration.ORIENTATION_UNDEFINED; // ORIENTATION_UNDEFINED = 0
public void onCreate(Activity activity, Bundle savedInstanceState) {
activity.onCreate(savedInstanceState);
current_orientation = activity.getResources().getConfiguration().orientation;
}
public boolean isDevicePortrait() { return current_orientation == Configuration.ORIENTATION_PORTRAIT; }
}
Activiti.java
public class Activiti extends Activity {
private ActivityUtil activityUtil;
@Override
public void onCreate(Bundle savedInstanceState) {
activityUtil.onCreate(this, savedInstanceState);
}
protected boolean isDevicePortrait() { return activityUtil.isDevicePortrait(); }
}
FragmentActiviti.java
public abstract class FragmentActiviti extends FragmentActivity {
private ActivityUtil activityUtil;
@Override
public void onCreate(Bundle savedInstanceState) {
activityUtil.onCreate(this, savedInstanceState);
}
protected boolean isDevicePortrait() { return activityUtil.isDevicePortrait(); }
}
在 ActivityUtil.onCreate()
中,activity.onCreate(savedInstanceState);
导致此编译错误:
The method onCreate(Bundle) from the type Activity is not visible.
如果我将 Activity
更改为 Activiti
:
public abstract class ActivityUtil {
public void onCreate(Activiti activity, Bundle savedInstanceState) { ... }
...
}
这将导致 FragmentActiviti.onCreate()
的 activityUtil.onCreate()
中的另一个编译错误:
The method onCreate(Activiti, Bundle) in the type ActivityUtil is not applicable for the arguments (FragmentActiviti, Bundle)
我明白为什么会出现这些错误。但我只是不知道如何避免它们。
感谢所有为这个问题做出贡献的人,特别是@flup 指导我了解 Decorator Pattern 、@hazzik 和@donramos 感谢你们的广泛努力,我在这里发帖
Activity
和 FragmentActivity
类。如果您也在开发 Android 应用程序,我希望我的代码可以在某些方面对您有所帮助:-)
ActivityCore.java
package xxx.android;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
public final class ActivityCore {
public interface ActivityCallbackInterface {
public void onCreateCallback(Bundle savedInstanceState);
public void onBeforeSaveInstanceState(Bundle outState);
public void onSaveInstanceStateCallback(Bundle outState);
}
private final Activity activity;
/**
* This current_orientation variable should be once set, never changed during the object life-cycle.
* But Activity.getResources() is not yet ready upon the object constructs.
* That's why THIS CLASS is wholly responsible to maintain THIS VARIABLE UNCHANGED.
*/
private int current_orientation = Configuration.ORIENTATION_UNDEFINED; // ORIENTATION_UNDEFINED = 0
public ActivityCore(Activity activity) { this.activity = activity; }
public void onCreate(Bundle savedInstanceState) {
((ActivityCallbackInterface) activity).onCreateCallback(savedInstanceState);
current_orientation = activity.getResources().getConfiguration().orientation;
}
public void onSaveInstanceState(Bundle outState) {
/**
* THIS is the best ever place i have found, to unload unwanted Fragments,
* thus prevent re-creating of un-needed Fragments in the next state of Activity.
* (state e.g. Portrait-to-Landscape, or Landscape-to-Portrait)
*
* The KEY is to do it BEFORE super.onSaveInstanceState()
* (my guess for this reason is, in super.onSaveInstanceState(),
* it saves the layout hierarchy, thus saved the Fragments into the Bundle also.
* Thus restored.
* Note that Fragments NOT IN LAYOUT, having ONLY TAGS, are also restored.)
*/
((ActivityCallbackInterface) activity).onBeforeSaveInstanceState(outState);
((ActivityCallbackInterface) activity).onSaveInstanceStateCallback(outState);
}
public int getCurrentOrientation() { return current_orientation; }
public boolean isDevicePortrait() { return current_orientation == Configuration.ORIENTATION_PORTRAIT; }
public boolean isDeviceLandscape() { return current_orientation == Configuration.ORIENTATION_LANDSCAPE; }
public boolean isNewDevicePortrait() { return activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT; }
public boolean isNewDeviceLandscape() { return activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE; }
public boolean isPortrait2Landscape() { return isDevicePortrait() && isNewDeviceLandscape(); }
public boolean isLandscape2Portrait() { return isDeviceLandscape() && isNewDevicePortrait(); }
public String describeCurrentOrientation() { return describeOrientation(current_orientation); }
public String getCurrentOrientationTag() { return getOrientationTag(current_orientation); }
public String describeNewOrientation() { return describeOrientation(activity.getResources().getConfiguration().orientation); }
public String getNewOrientationTag() { return getOrientationTag(activity.getResources().getConfiguration().orientation); }
private String describeOrientation(final int orientation) {
switch (orientation) {
case Configuration.ORIENTATION_UNDEFINED: return "ORIENTATION_UNDEFINED"; // 0
case Configuration.ORIENTATION_PORTRAIT: return "ORIENTATION_PORTRAIT"; // 1
case Configuration.ORIENTATION_LANDSCAPE: return "ORIENTATION_LANDSCAPE"; // 2
case Configuration.ORIENTATION_SQUARE: return "ORIENTATION_SQUARE"; // 3
default: return null;
}
}
@SuppressLint("DefaultLocale")
private String getOrientationTag(final int orientation) {
return String.format("[%d:%s]", orientation, describeOrientation(orientation).substring(12, 16).toLowerCase());
}
}
Activity.java
package xxx.android.app;
import xxx.android.ActivityCore;
import xxx.android.ActivityCore.ActivityCallbackInterface;
import android.os.Bundle;
public abstract class Activity extends android.app.Activity implements ActivityCallbackInterface {
private final ActivityCore activityCore;
public Activity() { super(); activityCore = new ActivityCore(this); }
@Override
protected void onCreate(Bundle savedInstanceState) { activityCore.onCreate(savedInstanceState); }
@Override public void onCreateCallback(Bundle savedInstanceState) { super.onCreate(savedInstanceState); }
@Override
public void onBeforeSaveInstanceState(Bundle outState) {} // Optionally: let child class override
@Override
protected void onSaveInstanceState(Bundle outState) { activityCore.onSaveInstanceState(outState); }
@Override public void onSaveInstanceStateCallback(Bundle outState) { super.onSaveInstanceState(outState); }
public final int getCurrentOrientation() { return activityCore.getCurrentOrientation(); }
public final boolean isDevicePortrait() { return activityCore.isDevicePortrait(); }
public final boolean isDeviceLandscape() { return activityCore.isDeviceLandscape(); }
public final boolean isNewDevicePortrait() { return activityCore.isNewDevicePortrait(); }
public final boolean isNewDeviceLandscape() { return activityCore.isNewDeviceLandscape(); }
public final boolean isPortrait2Landscape() { return activityCore.isPortrait2Landscape(); }
public final boolean isLandscape2Portrait() { return activityCore.isLandscape2Portrait(); }
public final String describeCurrentOrientation() { return activityCore.describeCurrentOrientation(); }
public final String getCurrentOrientationTag() { return activityCore.getCurrentOrientationTag(); }
public final String describeNewOrientation() { return activityCore.describeNewOrientation(); }
public final String getNewOrientationTag() { return activityCore.getNewOrientationTag(); }
}
FragmentActivity.java
package xxx.android.support.v4.app;
import xxx.android.ActivityCore;
import xxx.android.ActivityCore.ActivityCallbackInterface;
import android.os.Bundle;
public abstract class FragmentActivity extends android.support.v4.app.FragmentActivity implements ActivityCallbackInterface {
private final ActivityCore activityCore;
public FragmentActivity() { super(); activityCore = new ActivityCore(this); }
@Override
protected void onCreate(Bundle savedInstanceState) { activityCore.onCreate(savedInstanceState); }
@Override public void onCreateCallback(Bundle savedInstanceState) { super.onCreate(savedInstanceState); }
@Override
public void onBeforeSaveInstanceState(Bundle outState) {} // Optionally: let child class override
@Override
protected void onSaveInstanceState(Bundle outState) { activityCore.onSaveInstanceState(outState); }
@Override public void onSaveInstanceStateCallback(Bundle outState) { super.onSaveInstanceState(outState); }
public final int getCurrentOrientation() { return activityCore.getCurrentOrientation(); }
public final boolean isDevicePortrait() { return activityCore.isDevicePortrait(); }
public final boolean isDeviceLandscape() { return activityCore.isDeviceLandscape(); }
public final boolean isNewDevicePortrait() { return activityCore.isNewDevicePortrait(); }
public final boolean isNewDeviceLandscape() { return activityCore.isNewDeviceLandscape(); }
public final boolean isPortrait2Landscape() { return activityCore.isPortrait2Landscape(); }
public final boolean isLandscape2Portrait() { return activityCore.isLandscape2Portrait(); }
public final String describeCurrentOrientation() { return activityCore.describeCurrentOrientation(); }
public final String getCurrentOrientationTag() { return activityCore.getCurrentOrientationTag(); }
public final String describeNewOrientation() { return activityCore.describeNewOrientation(); }
public final String getNewOrientationTag() { return activityCore.getNewOrientationTag(); }
}
最后,我真的要感谢你们的帮助,并不断与我更新解决进度!你们都是使 stackoverflow 成为程序员完美站点的关键人物。如果您在我的代码中发现任何问题,或者有任何需要改进的地方,请随时再次帮助我:-)
因为onBeforeSaveInstanceState()
是在使用时实现的,所以三个类都需要保持抽象
。这导致成员变量 current_orientation
重复。如果 current_orientation
可以放入 class ActivityBase
中,或者将其分组到其他地方,那就更好了!
愚蠢的我。我已经修好了:-)
最佳答案
在我看来,这里最好的解决方案是将逻辑委托(delegate)给某个类,我们称它为 CustomActivityLogic
。
如果您想从逻辑类中访问 Activity 类的一些数据或方法,您还需要为您的 Activity 创建通用接口(interface) (CustomActivity
)。
要调用 protected 虚拟重写方法,有两种解决方案:
CustomActivity.java
public interface CustomActivity {
void someMethod();
}
Activiti.java
import android.app.Activity
public class Activiti
extends Activity
implements CustomActivity {
private CustomActivityLogic logic = new CustomActivityLogic();
public void someMethod() { /***/ }
public void myNewMethod() { logic.myNewMethod(this); }
@Override
protected void onCreate(Bundle savedInstanceState) {
logic.onCreate(this, savedInstanceState); // call shared logic
super.onCreate(savedInstanceState); // call super
}
}
FragmentActivitii.java
import android.support.v4.app.FragmentActivity;
public class FragmentActivitii
extends FragmentActivity
implements CustomActivity {
private CustomActivityLogic logic = new CustomActivityLogic();
public void someMethod() { /***/ }
public void myNewMethod() { logic.myNewMethod(this); }
@Override
protected void onCreate(Bundle savedInstanceState) {
logic.onCreate(this, savedInstanceState); // call shared logic
super.onCreate(savedInstanceState); // call super
}
}
CustomActivityLogic.java
public class CustomActivityLogic {
public void myNewMethod(CustomActivity activity) { /*...*/ }
public void onCreate(Activity activity, Bundle savedInstanceState) {
/* shared creation logic */
}
}
使 onCreate 可通过 CustomActivity
接口(interface)从外部调用的方法
CustomActivity.java
public interface CustomActivity {
void someMethod();
void onCreateSuper(Bundle savedInstanceState);
}
Activiti.java
import android.app.Activity
public class Activiti
extends Activity
implements CustomActivity {
private CustomActivityLogic logic = new CustomActivityLogic();
public void someMethod() { /***/ }
public void myNewMethod() { logic.myNewMethod(this); }
@Override
protected void onCreate(Bundle savedInstanceState) {
logic.onCreate(this, savedInstanceState); // call shared logic
}
public void onCreateSuper(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // call super
}
}
FragmentActivitii.java
import android.support.v4.app.FragmentActivity;
public class FragmentActivitii
extends FragmentActivity
implements CustomActivity {
private CustomActivityLogic logic = new CustomActivityLogic();
public void someMethod() { /***/ }
public void myNewMethod() { logic.myNewMethod(this); }
@Override
protected void onCreate(Bundle savedInstanceState) {
logic.onCreate(this, savedInstanceState); // call shared logic
}
public void onCreateSuper(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // call super
}
}
CustomActivityLogic.java
public class CustomActivityLogic {
public void myNewMethod(CustomActivity activity) { /*...*/ }
public void onCreate(CustomActivity activity, Bundle savedInstanceState) {
/* shared creation logic */
activity.onCreateSuper(savedInstanceState); // call-back super
}
}
关于android - 以最佳编码风格(最少重复)向 Android 的 Activity(父类)和 FragmentActivity(子类)添加功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15753269/
Textmate 语法(.tmLanguage 文件)有时以 XML 格式表示。 我想转换为更易读的格式(即 JSON 或 YAML)以集成到 VS Code 语法突出显示扩展中。 为了澄清我的意思,
如何通过 pandas 样式隐藏列标签?有一个 hide_index() 方法可以删除索引行,不幸的是 hide_column() 标签会删除整个列(标题和数据)。我只想隐藏标题。谢谢! 最佳答案 s
我正在考虑为一组服务使用 SOA 架构来支持我咨询的业务,以前我们使用数据库集成,其中每个应用程序从共享的 MS SQL 数据库中挑选出它需要的东西并使用它等等。我们有各种与怪物数据库(包括 java
所以我有以下代码,我想知道 Objective-C 中哪种“风格”被认为更好。 选项 1: id temp = [dictionary objectForKey: @"aBooleanValue"];
当创建一个没有类参数的对象时,我很难决定是否应该包含空括号。一个具体的例子:我正在与现有的 Java 代码交互,并创建一个实现名为 EventResponder 的接口(interface)的对象。我
我有一个抽象类Stack和一个扩展它的类:MyStack。我需要为 MyStack 创建一个复制构造函数。只传入 MyStack 对象更好,还是传入任何 Stack 对象更好? public MySt
我正在考虑将那些在函数体中未修改的 Python 函数参数拼写为 ALL_UPPERCASE,向此类 API 的用户发出信号,表明传递的值不会被修改(如果一切都如广告所言,无论如何) )。我不知道这会
我的 build.gradle 文件、staging、stable 和 production 以及默认构建类型 debug 和 release。对于其中的每一个,我都有不同的 AAR 文件,例如,我有
假设我有以下文件: main.cpp 例程.cpp 例程.h 进一步假设 main.cpp 调用了在 routine.cpp 中定义的函数 routine(),但是 routine.cpp 还包含仅由
我对此进行了一些搜索,但实际上我还没有找到 MySQL 中用于创建外键的样式概念是什么 - 在创建表定义中或在 alter 语句中。谢谢。 最佳答案 何时创建外键: 如果在创建表时明确需要外键,则在创
您好,我正在尝试将 Android 应用风格(免费且完整)实现为动态壁纸。在 Eclipse 中,我曾经使用以下代码从我自己的 Android Activity 打开动态壁纸预览: I
我的 Android 应用程序有两种不同的风格,lite 和 pro。在应用程序中,我有一个名为 customFragment.java 的类,它包含在 main 中(不同风格之间没有区别)并且还包含
我有一个包含多个子目录的项目,如下所示: /opt/exampleProject/src ├── __init__.py ├── dir1 │ ├── __init__.py │ ├──
假设我们有类似的东西 int f(int n); .... do{ int a = b; int b = f(a); } 这样说有没有风险 do{ int b = f(b);
是否有风格指导或理由来选择其中一种模式而不是另一种? 最小化上下文管理器下的代码量“感觉”更干净,但我无法指出具体原因。这可能只是偏好,并没有关于此事的官方指导。 1) 里面的所有代码都有上下文。 w
module Hints module Designer def self.message "Hello, World!" end
我正在开发一个具有多种风格的 android 项目。 这很好用,我可以自定义应用程序的元素,例如颜色和字符串资源。 我想让一些风格基于 AppCompat 浅色主题,一些基于 AppCompat 深色
因此,这不起作用,因为 seatsAvailable 是最终的。如何使用更多的 lambda 风格的从头开始的方式来完成我想要完成的事情? final boolean seatsAvailable =
考虑以下代码: cpu_set_t cpuset; CPU_ZERO(&cpuset); CPU_SET(0, &cpuset); sched_setaffinity(0, sizeof(cpuset
从历史上看,我总是这样编写我的异常处理代码: Cursor cursor = null; try { cursor = db.openCursor(null, null
我是一名优秀的程序员,十分优秀!