Search code examples
androidandroid-intentandroid-activityextendsintentfilter

Android: How to call a a Class within the Current Class


I'm working on an application that has multiple Activities, and I'm tired of running back and forth between the Manifest and XML layout and stuff. Is there a way to have

Intent intent = new Intent(MainActivity.this, MainActivity.Settings.class);

Or something? Because I've tried it, it doesn't throw me an error, but it just force closes the application. I'm able to bundle all my classes from different .java into one, for ex.

public class MainActivity extends Activity
{
    ...
    @Override
    protected void onCreate(Bundle MainActivityState)
    {
        ...
    }

    public class Settings extends ...
    {
        ...
    }

    public class Register extends ...
    {
        ...
    }

    public class Login extends ...
    {
        ...
    }

    public class BeautifulLady extends personality ...
}

Solution

  • Simple. Just don't even try.

    An activity loosely represents a single screen - something the user interacts with. Android is built around this concept and trying to circumvent it will lead to tears.

    Stick with it. Having your classes in separate files, and having layout XML separate for each activity, will become your friend and will actually speed things up once you are familiar.

    Start with the Activity life cycle document and read it several times until the penny drops. Then expand out from there.

    http://developer.android.com/reference/android/app/Activity.html

    Object oriented programming, with classes that take care of themselves, is a joy and regardless of which platform you choose to develop on is the way to go for the foreseeable future (old hands, no debates on OOP vs functional please ;)).

    If you are going to do mobile development, then the separation of activities, classes and UI is the same concept, just done differently.

    See also MVC programming and its' cousins.

    http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

    Good luck.