简体   繁体   English

startActivity(intent)之后活动未开始

[英]Activity doesn't start after startActivity(intent)

I am working on the driver application of a cab/taxi booking system. 我正在研究出租车/出租车预订系统的驾驶员应用程序。 Application has foreground service. 应用程序具有前台服务。 Service starts to work with this code in MainActivity: 服务开始在MainActivity中使用以下代码:

Intent myService = new Intent(getApplicationContext(), OnLineForegroundService.class);
startForegroundService(myService);

This service starts NewOrderActivity by trigger using the following code: 该服务使用以下代码通过触发来启动NewOrderActivity:

Intent intent = new Intent(OnLineForegroundService.this, NewOrderActivity.class);
intent.putExtra("newOrder", newOrder);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                      
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);

After that in NewOrderActivity application starts TripActivity (by user tap): 之后,在NewOrderActivity中,应用程序启动TripActivity(通过用户点击):

//some finish service code
...

Intent intent = new Intent(NewOrderActivity.this, TripActivity.class);
intent.putExtra("order", newOrder);
startActivity(intent);
finish();

and the last step is the end of the trip: 最后一步是旅程的终点​​:

//TripActivity

Intent i = new Intent(TripActivity.this, MainActivity.class);
i.putExtra("showOnLineDialog", true);
startActivity(i);
finishAffinity();

在此处输入图片说明

These actions are performed cyclically. 这些动作是循环执行的。 Sometimes (very rarely) NewOrderActivity doesn't start after startActivity(intent) . 有时(很少)NewOrderActivity在startActivity(intent)之后不启动。 Even code in onCreate(Bundle savedInstanceState) or onResume() in NewOrderActivity does not perform. 甚至NewOrderActivity中的onCreate(Bundle savedInstanceState)onResume()中的代码也不执行。 Just nothing happens in the case when some activity is in the foreground. 当某些活动处于前台时,什么也不会发生。 When the application is in the background (except foreground service) - application goes to the foreground and shows last opened activity but not NewOrderActivity. 当应用程序处于后台(前台服务除外)时,应用程序将转到前台并显示上次打开的活动,但不显示NewOrderActivity。 What could be the problem? 可能是什么问题呢? I suppose that problem may be in Activity lifecycle and flags ( FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_NO_HISTORY ) but not sure about it. 我认为问题可能出在活动生命周期和标志( FLAG_ACTIVITY_NEW_TASKFLAG_ACTIVITY_NO_HISTORY )中,但不确定。

Manifest: 表现:

<application
    android:name=".TaxiApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".ui.activities.SplashActivity"
        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=".ui.activities.MainActivity"
        android:screenOrientation="portrait" />
    <activity
        android:name=".ui.activities.NewOrderActivity"
        android:screenOrientation="portrait" />
    <activity
        android:name=".ui.activities.TripActivity"
        android:screenOrientation="portrait" />
    <service android:name=".services.OnLineForegroundService" />
</application>

I don't think starting an activity from a service with the service's context is a good idea, try using application context. 我认为从具有服务上下文的服务开始活动不是一个好主意,请尝试使用应用程序上下文。 Change this: 更改此:

Intent intent = new Intent(OnLineForegroundService.this, NewOrderActivity.class);

to this: 对此:

Intent intent = new Intent(getApplicationContext(), NewOrderActivity.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM