简体   繁体   English

如何在多种 Android 活动类型之间共享方法

[英]How to share methods across multiple Android activities types

I'm building and Android application using ActionBarSherlock .我正在使用ActionBarSherlock构建Android应用程序。

I have multipe types of activities:我有多种类型的活动:

  • SherlockFragmentActivity SherlockFragmentActivity
  • SherlockMapActivity夏洛克地图活动
  • SherlockActivity夏洛克活动

I want all my activities to share common methods to follow an internal workflow of screens.我希望我的所有活动都共享通用方法来遵循屏幕的内部工作流程。

If I create a Workflow class that extends SherlockFragmentActivity then my MapActivity does not work anymore.如果我创建一个扩展SherlockFragmentActivityWorkflow类,那么我的MapActivity does不再工作。 If I create a Worflow class that extends SherlockMapActivity then my TutorialActivity does not work anymore (because it's using a new SectionsPagerAdapter(getSupportFragmentManager()); .如果我创建了一个扩展SherlockMapActivityWorflow类,那么我的TutorialActivity不再工作(因为它使用了一个new SectionsPagerAdapter(getSupportFragmentManager());

Do note that the common methods I want are also running startActivity() .请注意,我想要的常用方法也在运行startActivity()

I know Java cannot have a class that extends more than one class, so how should I go about this?我知道 Java 不能有一个扩展多个类的类,那么我应该怎么做呢?

public class Workflow extends SherlockMapActivity {

protected void goMain() {
    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);
}

@Override
protected boolean isRouteDisplayed() {
    return false;
}

    // ...
}

public class TutorialActivity extends Workflow {
    // ...
    // new SectionsPagerAdapter(getSupportFragmentManager());
    // ...
}

public class GameActivity extends Workflow {
    // ...
    // MapView
    // ...
}

I also want to share code like this:我也想分享这样的代码:

@Override
protected void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setProgressBarIndeterminate(true);
    setProgressBarIndeterminateVisibility(false);
}

You can create a custom Activity class and extend that in your activities.您可以创建自定义Activity类并在您的活动中扩展它。

So for example:例如:

public abstract class DerivedActivityA extends Activity implements CustomInterfaceA {

    // ... your code here
}

public abstract class DerivedActivityB extends DerivedActivityA implements CustomInterfaceB {

    // ... your code here
}

If you have to implement multiple interfaces then use abstract classes like I did above and implement interfaces.如果你必须实现多个接口,那么像我上面所做的那样使用抽象类并实现接口。

Edit: If I get it right SectionsPagerAdapter is just an adapter so you can compose it in one of your classes as a field.编辑:如果我做对了SectionsPagerAdapter只是一个适配器,因此您可以将它作为一个字段组合在您的一个类中。

Edit2: You can't extend two classes and you can't compose an Activity into another one so you either have to write things for yourself or extract the functionality you need from your third party library. Edit2:您不能扩展两个类,也不能将一个Activity组合成另一个类,因此您必须自己编写内容或从第三方库中提取所需的功能。

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

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