Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Application vs Activity

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

like image 900
Unpossible Avatar asked Jul 21 '11 11:07

Unpossible


People also ask

What is the difference between application and activity in Android?

Application class is an optional facility for extending and storing application-global state. There are other ways of doing this, so most apps don't customize this class. Activities however are what defines every major stage of your application. It wouldn't be possible to build an application without Activities.

What is the difference between activity and application?

They are both instances of Context, but the application instance is tied to the lifecycle of the application, while the Activity instance is tied to the lifecycle of an Activity. Thus, they have access to different information about the application environment.

What is an activity in Android application?

An activity provides the window in which the app draws its UI. This window typically fills the screen, but may be smaller than the screen and float on top of other windows. Generally, one activity implements one screen in an app.

Can Android application have activity without layout?

The answer is yes it's possible. Activities don't have to have a UI. It's mentioned in the documentation, e.g.: An activity is a single, focused thing that the user can do.


1 Answers

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;
    }
}
like image 144
inazaruk Avatar answered Sep 17 '22 20:09

inazaruk