0

I am looking for advice when it comes to properly handling flows between Activities. Let's say I have two activities - LoginActivity and MainActivity:

<activity
android:name=".LoginActivity"
android:configChanges="keyboardHidden|orientation"
android:label="@string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait">
<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation"
android:label="@string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait" />

Now, when application starts, I start LoginActivity, and if login is successful I show MainActivity.

Now, the problems start when I want to handle certain types of Intents. Let's say I want to add custom schema to my app:

<intent-filter>
    <data android:scheme="customschema" />
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.BROWSABLE" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

To which activity would I add this? Ideally I would love to add it to MainActivity and then handle it in onNewIntent method, but it is possible that my app was not started and in that case, I first need to run LoginActivity. On the other hand if I add that to LoginActivity, I need a way to pass that intent to MainActivity once user is successfully logs in. Or if app is already started I need to switch focus from LoginActivity to MainActivity and pass intent data immediately.

I believe this problem must've been solved previously and I would much rather follow best practice rather than try to invent my own solution - which is why I am looking for advice.

nikib3ro
  • 20,366
  • 24
  • 120
  • 181

2 Answers2

1

Forget what I said initially, I was thinking of Broadcast Receivers.

Use this solution by Commonsware instead:

How to create/disable intent-filter by programmable way?

Community
  • 1
  • 1
Stephan Branczyk
  • 9,363
  • 2
  • 33
  • 49
1

Your MUST never have more then a ONE LoginActivity instance.

Once you've received intent to your "Main activity" make sure in the onCreate method to check for the login, and if the user is not logged in, throw an intent back to the LoginActivity.

if the user have reached the LoginActivity, but is already logged in throw an intent back to the main activity.

This might cause a glitches in the UI, but that would only indicate a faulty design, since no event of any kind should arrive to an application which is not logged in!

TacB0sS
  • 10,106
  • 12
  • 75
  • 118
  • I don't want to have more than one LoginActivity instance - I was just debating on different scenarios on how I could approach this. Sorry, if I was not clear on that. – nikib3ro Mar 25 '13 at 23:54
  • what about the rest of the solution... why doesn't it fit your needs? – TacB0sS Mar 25 '13 at 23:57