Nested inner Activity class in android

Falmarri picture Falmarri · Oct 29, 2010 · Viewed 10.1k times · Source

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.

Answer

DustinB picture DustinB · Feb 23, 2012

Yes, it does work -- it is just another class -- you just need to declare your activity using inner class notation in AndroidManifest.xml:

<activity android:name=".ListClass$ItemClass"/>

Seems to work fine for me, but perhaps when this question was asked, it wasn't supported in older versions of Android?

Not sure WHY you'd want to do this, but you can.