Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6153665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:07:50+00:00 2026-05-23T20:07:50+00:00

I have written a few Android apps, and have always declared a starting Activity

  • 0

I have written a few Android apps, and have always declared a starting Activity as the:

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

It would be great for scoping some global methods, statics, shared prefs, etc if I could start my app using an Application that then calls the first Activity from it’s onCreate() after setting up prefs, etc, but I haven’t been able to find any examples of this design pattern… when I try this in code, I get a ClassCastException:

public class MyApplication extends Application {
@Override
    public void onCreate() {
        super.onCreate();

        // do stuff (prefs, etc)

        // start the initial Activity
        Intent i = new Intent(this, InitialActivity.class);
    startActivity(i);
    }
}

InitialActivity.class is indeed an Activity that works fine if I set it to be MAIN, but trying to start it from MyApplication that is declared MAIN generates the error. Probably a very silly question, but am I tackling this all wrong?

Thanks,

Paul

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:07:51+00:00Added an answer on May 23, 2026 at 8:07 pm

    You can fix this by using FLAG_ACTIVITY_NEW_TASK flag:

    Intent intent = new Intent(this, ApplicationActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    

    That’s because you need to start new task when Activity is started outside of Activity context. But I strongly recommend to not start Activity from your Application’s onCreate().


    Android has 4 components: Activity, Service, ContentProvider and Broadcast.

    When Android needs to activate one of this components from your application, it looks if there is already existing running process with your application. If not, then Android starts new process, initializes it, then it initializes your custom Application instance. And then it activates one of needed components.

    Now, let’s consider next scenario: your application declared content provider in AndroidManifest.xml, and Android just about to start your application so you can provide some data to another foreground application.

    1. Content Provider request is sent
    2. Your application wasn’t running, and Android starts new process for it.
    3. Your custom Application instance is created
    4. Application.onCreate() is called.
    5. You start an activity
    6. Your Content Provider receives request

    Somebody just wanted to connect to your content provider, but your application started an Activity instead. Same true for starting background Service and sometimes broadcast receivers.

    And also consider if some other application’s activity A wanted to started activity X from your application. But in onCreate() you started activity Y, and then X is also started by Android. Then user presses back. What should happen? Its tricky…

    Starting activities from Application‘s onCreate may result in quite weird user experience. So don’t do it.


    UPDATE:
    Because Android guarantees that Application will be created only once and before any other component, you can use next code to access your Application’s single instance:

    public class MyApplication extends Application 
    {   
        private static MyApplication s_instance;
    
        public MyApplication()
        {
            s_instance = this;
        }
    
        public static MyApplication getApplication()
        {
            return s_instance;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a few apps for the iphone now, but they are all
I have written a few MSBuild custom tasks that work well and are use
I have written a few modules of code in Access vba. Each code runs
I have written a few Python tools in the past to extract data from
I have written a few Excel 2003/2007 add-ins using VSTO, and I usually end
I have written a few nearly identical functions, except for their names. For example:
I have written quite a few Ruby programs, but if I need to show
I have written a mini web-server which needs to serve up a few static
I am learning Python for the past few days and I have written this
I have a few Windows Services written in C# that I have setup to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.