简体   繁体   English

使用Service在Android中恢复活动

[英]resuming activity in Android using Service

I have a mainActivity . 我有一个mainActivity
When starts, it starts a service and bonds it. 启动时,它将启动服务并将其绑定。
There is a timer which will send the mainActivity (this) to back after X seconds, while the service keep running and listening, i use moveTaskToBack (true) . 有一个计时器,它将在X秒后将mainActivity (this)发送回去,同时该服务保持运行和侦听,我使用moveTaskToBack (true)
When the service listener triggered, the service starts the activity, but instead of the activity to be called through onResume() (since it was sent to back) its called through onCreate() , to call the activity currently i use : 当服务侦听器触发时,服务将启动活动,但不是通过onResume()调用活动(因为它已发送回),而是通过onCreate()调用了该活动,以调用当前使用的活动:

Intent dialogIntent = new Intent(getBaseContext(), MainActivity.class);
dialogIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
getApplication().startActivity(dialogIntent);

Which explains why the activity is created all over again. 这说明了为什么要重新创建活动。 I tried using FLAG_ACTIVITY_REORDER_TO_FRONT but got an exception. 我尝试使用FLAG_ACTIVITY_REORDER_TO_FRONT但是遇到了异常。

add this attribute in activity of AndroidManifest.xml android:launchMode="singleInstance" in AndroidManitest.xml 在AndroidManifest.xml的活动中将此属性添加到AndroidManitest.xml中的android:launchMode="singleInstance"

AndroidMenifest.xml AndroidMenifest.xml

 <application>
        <activity
            android:launchMode="singleInstance"> 
        </activity>
    </application>

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

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