Android的免费/付费 - 使用架构库
我需要创建应用程序的免费和付费版本,因此我使用与共同活动和资源库项目但是我想知道你将如何去组织这样的:
I need to create a free and paid version of the app and therefore I am using a library project with the common activities and resources in. However I was wondering how you would go about organising this:
当用户点击一个按钮共享(库)活性 MyActivity.java
启动一个DialogFragment。该DialogFragment将是免费和付费的应用程序不同。对于免费的应用程序,我们将推出 FreeDialog.java
和付款,我们将推出 PaidDialog.java
。这两个对话框碎片将不会在因为他们没有共享库项目;他们将在单独的免费和付费的项目。
A shared (library) activity MyActivity.java
launches a DialogFragment when a user clicks a buttons. The DialogFragment will be different for the free and paid apps. For the free app we will launch FreeDialog.java
and for paid we will launch PaidDialog.java
. These two dialog fragments will not be in the library project as they are not shared; they will be in the free and paid projects separately.
我能想到的唯一的事情就是让 MyActivity.java
抽象的,它的免费和付费项目延伸。这些扩展将推出各自的DialogFragments。
The only thing I can think is to make MyActivity.java
abstract and to extend it in the free and paid projects. The extensions will launch their respective DialogFragments.
的另一种方式可以是重载的片段中的每个项目。不过,我不知道这是可能的。
Another way could be to overload the Fragments in each of the projects. However, I am not sure if this is possible.
从理论上说,你可以把 MyActivity
抽象的,它的子类在付费和免费的应用程序项目。但是,活动
类是比普通的Java类,它的复杂性这一点不同。他们在AndroidManifest.xml列出,然后也没有通过简单的 newing创建的他们。他们通常与意图
取值创建。所以,我想我会从制作活动
一个抽象类望而却步。
Theoretically, you could make MyActivity
abstract, and subclass it in both paid and free app projects. But, Activity
classes are a little different than normal Java classes, which complicates this. They are listed in AndroidManifest.xml, and then also are not created by simply newing them up. They're normally created with Intent
s. So, I think I would stay away from making the Activity
an abstract class.
我觉得你真正想要的是使用的类似的工厂模式的创建 DialogFragment
,其中您共同库不知道哪个 DialogFragment
将创建的实例。你可以得到pretty看中了一个工厂,你可以阅读所有关于其他地方,但一个简单的人们可能会为你工作:
I think what you really want is to use is something like the Factory pattern to create an instance of DialogFragment
, where your common library does not know which DialogFragment
will be created. You can get pretty fancy with a factory, and you can read all about that elsewhere, but a simple one might work for you:
public class FragmentFactory {
private static String _dialogFragmentClassName;
public static void registerFragment(String className) {
// you might choose to assert if _dialogFragmentClassName != null
_dialogFragmentClassName = className;
}
public static DialogFragment createFragment() {
try {
return (DialogFragment) Class.forName(_dialogFragmentClassName).newInstance();
} catch (Exception e) {
return null;
}
}
}
然后,在你的免费和付费的应用项目,code,你会发出在启动时调用这样的(例如,在主活动
)
FragmentFactory.registerFragment(com.mycompany.free.FreeDialogFragment.class.getName());
和
FragmentFactory.registerFragment(com.mycompany.paid.PaidDialogFragment.class.getName());
最后,在公用库code,可以通过调用创建片段的实例
Finally, in the common library code, you can create an instance of the fragment by calling
DialogFragment fragment = FragmentFactory.createFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_content, fragment);
ft.commit();