-1

I have this situation in my app: I have these activities

<activity
            android:name=".presentation.view.start.view.activity.StartActivity"
            android:screenOrientation="sensorPortrait"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".presentation.view.main.view.activity.MainActivity"
            android:configChanges="locale"
            android:screenOrientation="sensorPortrait"
            android:theme="@style/AppThemeMultiStep"
            android:windowSoftInputMode="stateAlwaysHidden" />
        <activity
            android:name=".presentation.view.firstScreen.view.activity.FirstScreenActivity"
            android:screenOrientation="sensorPortrait"
            android:theme="@style/AppThemeMultiStep"
            android:windowSoftInputMode="stateAlwaysHidden" />
        <activity
            android:name=".presentation.view.signup.view.activity.LoginViaPinActivity"
            android:screenOrientation="sensorPortrait"
            android:theme="@style/AppThemeMultiStep" />

StartActivity acts as a sort of "router" based on the application state:

  • If I am not logged in, it will launch FirstScreenActivity
  • If I am logged in, it will launch LoginViaPinActivity, which will login the user based on some logic and then launch MainActivity. At the end of all, MainActivity will be at the root of the activities stack.

At some point the app will receive a notification, and when I tap it I want this:

if the app is running and MainActivity is running, open MainActivity (this is easy, there are planty of ways I can to that with various flags) but if it's not running launch StartActivity (so that I can open the app based on all the startup logics implemented there).

So the options I can think of are:

  • know if an activity is running in order to fire an intent or another (I don't like static fields solutions like you read in many SO post related to this)

  • make StartActivity the root of the task and find a combination of intent flags which will act like "launch StartActivity, but if it is running at the root of a task, bring that task to front" (this would be my preferred option if possible)

  • any suggestion is very appreciated

How do you usually handle this kind of situations? (I don't think I'm the first in the world with this problem :) )

Apperside
  • 3,542
  • 2
  • 38
  • 65
  • What I understood is you want to know if there is an activity running or not? Correct me if I'm wrong. – Paresh P. Mar 16 '17 at 09:04
  • yes, that's one of the options, but is not the best solution. Every thread speaking about detecting if an activity is running, the solution at the end is a static field – Apperside Mar 16 '17 at 09:38
  • Are you familiar with `onNewIntent()` ? – Paresh P. Mar 16 '17 at 09:49
  • Yes, I know how to use that method, but what I want is a suggestion about which is the best way to handle my situation – Apperside Mar 16 '17 at 15:44

1 Answers1

0

Here is my approach -

Make StartActivity as your router as you've said. Just make launchmode to singleTop in your manifest in order to user onNewIntent() method of an Activity.

You'll generate notification, sending some data with contentIntent - as a result clicking on notification you'll be redirected to StartActivity.

Two cases here -

  • If StartActivity is in stack
    onNewIntent gets called with your new Intent - Choose your activity required to be open -
    If it is already in stack Bring it to front using FLAG_ACTIVITY_REORDER_TO_FRONT flag

  • If StartActivity is not running or not in stack
    Receive bundle of intent that is being got from the intent via notification, parse data and open an activity accordingly.

Paresh P.
  • 6,677
  • 1
  • 14
  • 26