Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No need to register base Activity class in the Manifest?

I have my own base abstract class which extends Activity class.

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.

Is this a time bomb (not registering base class in Manifest) or this is the way it should be? Can someone explain why?

like image 735
sandalone Avatar asked May 18 '12 13:05

sandalone


2 Answers

You dont need to register BaseActivity class in manifest because its not the one you call in intent to launch.

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.

like image 64
waqaslam Avatar answered Oct 06 '22 15:10

waqaslam


According to the docs, the <activity> on the manifest:

Declares an activity (an Activity subclass) that implements part of the application's visual user interface. 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. Regardless of how you reach that activity. This excludes classes that only extend the Activity class but you can't reach directly.

Source

like image 16
Juan Cortés Avatar answered Oct 06 '22 17:10

Juan Cortés