0

I'm looking for a way to get the intent for my android app in my Application implementation. The Application class itself has no getIntent() just Activities seems to have this ability.

The reason for this is I have an app with several intents, which can be called stateless. But I need that the user is logged in. So I would like to check if the user is logged in and if not promt the login dialog, without hacking the same code in every activity.

1
  • lets all intents will "catched" by the same Activity - "chooser" ...in onCreate of this Activity you ll choose/start "real" Activity(with the same Intent) and finish() Activity "chooser" ... you can also put special Extra to Intent passed from "chooser" to "real" which indicates if user is logged and make login specific stuff only in "chooser" Acticity
    – Selvin
    Apr 10, 2013 at 8:58

2 Answers 2

1

The way to have some functionality that's in every Activity is to make them all inherit a common superclass, something like this:

class LoginActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        if (getIntent().hasExtra(...)) {
            doLogin();
        }
    }

class MyActivity extends LoginActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // user is now logged in
        setContentView(...);
    }
}

The LoginActivity is only there for your other activities to inherit, so don't mention it in your AndroidManifest.xml. You can't get the Intent in your Application because such a thing would make no sense. The Application object exists for as long as your process is running. At any given time there may be zero, one, or more than one of your activities live, so what Intent would it return?

I must admit I'm a bit puzzled about what the launching Intent has to do with logging in, though. It seems like you need a global object with the session state/credentials, rather than anything to do with intents.

1
  • I like the idea with inheritance. So is the logic in the common base class +1.
    – rekire
    Apr 10, 2013 at 9:16
0

You need to call getIntent() of your Launcher activity

1
  • As I already said I have multiple "launchers" and I want to avoid to change every activity.
    – rekire
    Apr 10, 2013 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.