Android处理点击通知
我的应用程序中有这样的情况:我有这些活动Android处理点击通知,android,android-intent,activity-stack,Android,Android Intent,Activity Stack,我的应用程序中有这样的情况:我有这些活动 <activity android:name=".presentation.view.start.view.activity.StartActivity" android:screenOrientation="sensorPortrait" android:theme="@style/SplashTheme"> <intent-filter&
<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根据应用程序状态充当一种“路由器”:
- 如果我没有登录,它将启动FirstScreenActivity
- 如果我登录,它将启动LoginViaPinActivity,它将根据某些逻辑登录用户,然后启动MainActivity。最后,MainActivity将位于activities堆栈的根
- 知道某个活动的运行是否是为了激发某个意图(我不喜欢静态字段解决方案,就像你在许多与此相关的帖子中读到的那样)
- 将StartActivity设置为任务的根目录,并找到意图标志的组合,其行为类似于“启动StartActivity,但如果它在任务的根目录下运行,则将该任务置于最前面”(如果可能,这将是我的首选选项)
- 非常感谢您的任何建议
StartActivity
作为您的路由器。只需在清单中将launchmode设置为singleTop
,即可使用活动的onNewIntent()
方法。
您将生成通知,使用contentIntent
发送一些数据-因此,单击通知,您将被重定向到StartActivity
。
这里有两个案例-
- 如果
StartActivity
在堆栈中
onNewIntent
以您的新意图被调用-选择需要打开的活动-
如果它已经在堆栈中,请使用标记\u activity\u REORDER\u to \u front
标记将其放在前面
- 如果
StartActivity
未运行或未在堆栈中
接收通过通知从intent获得的intent的捆绑包
,解析数据并相应地打开活动
我的理解是,您想知道是否有活动正在运行?如果我错了,请纠正我。是的,这是一种选择,但不是最好的解决方案。每个线程都在谈论检测活动是否正在运行,最后的解决方案是一个静态字段您是否熟悉onNewIntent()
?是的,我知道如何使用该方法,但我想要的是一个关于处理我的情况的最佳方法的建议