views:

68

answers:

2

Is declaring a class that extends Activity inside another Activity class possible? If it is, how would I register that class in the manifest? Also, is that something that can be reasonably done or is it a bad idea?

I was thinking of something like

class ListClass extends ListActivity{

    ...
    ArrayList items;

    class ItemClass extends Activity{

        ...
        Item item;

        @Override
        onCreate(){
            Integer pos = getIntent().getExtras().getInt("pos");
            item = items.get(pos);
        }
    }

    @Override
    onItemClick(int position){

        startActivity(new Intent(this, ItemClass.class).putExtra("pos", position));

    }
}

Note the syntax isn't 100% correct obviously, mostly pseudocode.

A: 

No, that's not possible. After all, the Android operating system will need to instantiate the Activity if it is started at any point (say, if you start it through an intent), and it's impossible to instantiate an ItemClass without a parent ListClass.

Remember that each Activity is completely independent and can be started at any point through an intent.

EboMike
A: 

I'd also be curious why you'd want to do this.

However, I don't see any reason why it wouldn't work. Couldn't you reference it in the AndroidManifest as you normally would as long as both classes are public? i.e. com.falmarri.ListClass.ItemClass?

Edit: Nevermind, this doesn't work as EboMike pointed out.

Brandon
How would the operating system instantiate the ItemClass without the ListClass? It's not a static class.
EboMike
Right, and then he won't be able to access ListClass's instance variables which is why he wanted to do it in the first place, so it defeats the purpose. Turns out it doesn't work for static classes anyway.
Brandon