我的应用程序架构包括启动程序活动中的启动画面。app启动后,闪屏被销毁。因此,为了防止应用程序重启(因为启动程序活动被销毁),我在闪屏的onCreate方法中添加了以下内容:
if (!isTaskRoot())
{
finish();
return;
}因此,如果已经有正在运行的实例并显示正在运行的实例,则新的实例将自动结束。到目前为止还不错。然而,新的应用实例并不总是从launcher启动。我已将Firebase消息设置为从服务器接收推送消息。推送消息包含一些额外的数据,可以这样访问:
String messageId = getIntent().getStringExtra("id_message");在点击推送消息通知后启动新的应用实例时。在应用程序已经运行的情况下,我需要在推送通知点击后显示它(它可以工作),然后处理已经运行的应用程序实例的message_id (新实例应该会自动完成)。但我不知道如何将message_id传递给已经在运行的实例,以便能够在不重启整个应用的情况下处理它并显示消息详细信息。
你有什么想法吗?
发布于 2017-10-18 11:42:54
您可以使用中间活动来选择要启动的活动。
首先创建,然后创建LaunchActivity
public class LaunchActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.empty);
BaseApp application = (BaseApp) this.getApplication();
// determine where to go
if(application.isHasGoToMain()) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(intent);
} else {
Intent intent = new Intent(this, SplashScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(intent);
}
}
}使用empty布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
</LinearLayout>对于第二个,如果主活动已启动,则创建要保存的应用程序:
public class BaseApp extends Application {
private boolean hasGoToMain = false;
public boolean isHasGoToMain() {
return hasGoToMain;
}
public void setHasGoToMain(boolean hasGoToMain) {
this.hasGoToMain = hasGoToMain;
}
}您需要在闪屏活动中设置该标志。
对于第三个,将LaunchActivity设置为AndroidManifest.xml中的启动器,并将其设置为hidden和singleTask
<activity
android:name=".LaunchActivity"
android:screenOrientation="portrait"
android:configChanges="orientation"
android:launchMode="singleTask"
android:theme="@android:style/Theme.NoDisplay"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>Fourth,你需要创建你的闪屏和主要活动作为singleTask:
<activity
android:name=".SplashScreenActivity"
android:launchMode="singleTask"
android:configChanges="screenSize|orientation|screenLayout">
</activity>
<activity
android:name=".MainActivity"
android:launchMode="singleTask"
android:label="@string/app_name"
android:theme="@style/AppThemeMain.NoActionBar">上一次启动时,需要在启动活动后设置该标志,设置方式为:
public class SplashScreenActivity extends AppCompatActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
BaseApp baseApp = (BaseApp) this.getApplication();
baseApp.setHasGoToMain(true);
}
}现在,您可以发送意图并在主活动的onNewIntent()中处理它。
发布于 2017-10-17 18:32:43
首先在清单文件中活动标记中添加启动模式,如
<activity
android:name="your_activity_name"
android:launchMode="singleTop" /> 当您单击通知并启动任何活动时
如果您的活动不在那里,则会开始一个新的活动。如果该活动已经打开,那么它的onNewIntent(Intent intent)将被调用,您将收到在此intent参数中发送的数据。
发布于 2017-10-17 18:33:19
当在应用程序中收到来自firebase的推送时,将由MyFirebaseMessagingService.处理在那里,您需要创建一个挂起的意图,即当用户点击通知时需要打开的活动。
Intent resultIntent = new Intent(context, ActivityToOpen.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntentWithParentStack(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);一旦用户单击通知,他们将被转移到activity ActivityToOpen.In,您可以将以下代码放入该活动
Intent upIntent =
NavUtils.getParentActivityIntent(ActivityToOpenOnBackPress.this);
if (NavUtils.shouldUpRecreateTask(CurrentActivityName.this,
upIntent))
{
TaskStackBuilder.create(CurrentActivityName.this)
.addNextIntentWithParentStack(upIntent)
.startActivities();
} else {
NavUtils.navigateUpTo(CurrentActivityName.this, upIntent);
}将这段代码放入onBackPressed函数中,或者在工具栏导航中,单击listener.So,当用户按下back按钮时,将创建一个人工堆栈,由android处理剩下的事情。
在androidmanifest.xml中,通过添加以下代码为每个活动分配一个父活动
android:parentActivityName=".ParentActivityName"
https://stackoverflow.com/questions/46787686
复制相似问题