简体   繁体   English

不需要在Manifest中注册基本Activity类吗?

[英]No need to register base Activity class in the Manifest?

I have my own base abstract class which extends Activity class. 我有自己的基本抽象类,它扩展了Activity类。

public abstract class BaseActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResourceId());
    }

    protected abstract int getLayoutResourceId();
}

public class Activity1 extends BaseActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // do extra stuff 
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.layout_for_activity1;
    }
}

My base class BaseActivity is not registered in the Manifest file and I do not get any error. 我的基类BaseActivity未在Manifest文件中注册,我没有收到任何错误。

Is this a time bomb (not registering base class in Manifest) or this is the way it should be? 这是一个定时炸弹(没有在Manifest中注册基类)或者它应该是这样的吗? Can someone explain why? 有人可以解释原因吗?

According to the docs, the <activity> on the manifest: 根据文档,清单上的<activity>

Declares an activity (an Activity subclass) that implements part of the application's visual user interface. 声明一个实现应用程序可视用户界面的一部分的活动(一个Activity子类)。 All activities must be represented by elements in the manifest file. 所有活动必须由清单文件中的元素表示。 Any that are not declared there will not be seen by the system and will never be run . 任何 未在此处声明的内容都不会被系统看到并且永远不会被 运行

Think about it like this: If there's an activity (any class that extends Activity or a class that extends it) that you will navigate to at some point in your application, it needs to be declared in the manifest. 想想这样:如果有一个活动(任何扩展Activity的类或扩展它的类),你将导航到应用程序的某个点,它需要在清单中声明。 Regardless of how you reach that activity. 无论你如何达到这项活动。 This excludes classes that only extend the Activity class but you can't reach directly. 这不包括仅扩展Activity类但无法直接访问的类。

Source 资源

You dont need to register BaseActivity class in manifest because its not the one you call in intent to launch. 不需要注册BaseActivity类清单中,因为它不是你的意图调用一个推出。

for example: 例如:

Intent i = new Intent(context, Activity1.class);
startActivity(i);

In above code, you need to have Activity1 activity registered in manifest because you are mentioning it in intent, not the BaseActivity class. 在上面的代码中,您需要在清单中注册Activity1活动,因为您在intent中提及它, 而不是 BaseActivity类。

不需要在manifest中注册Base类,因为它是一个抽象类,我们不能创建Abstract类的对象,我们只能定义它的方法和声明。

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

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