把我的活动回到顶端?我的

2023-09-07 11:26:44 作者:17.不悔初衷

此问题是分拆从How关闭或停止外部应用程序,我开始在Android的。

我正在写一个Android应用程序是一个远程控制工业过程 - 这个过程运行在PC,这与我的Andr​​oid应用程序不断的沟通上,偶尔PC机发送PDF文件到Android和我启动该AdobeReader.apk以显示它。当PC驳回形象我想关闭它在Android。

在我上面的链接被告知,一旦我启动AdobeReader没有办法从我的code将其关闭。 然而我也许能带给我的应用程序回到前面,这是一样好,我的目的。但我一直没能得到它的工作。我的应用程序的主要活动是RemoteControlActivity,我尝试:

 尝试{
   意图I =新的意图(CTX,RemoteControlActivity.class);
   ctx.startActivity(ⅰ);
}
   赶上(ActivityNotFoundException E){
   Log.d(ShowButtons(正常),隐藏);
}
 
怎样让qq好友动态返回顶端和微信朋友圈返回顶端

我也尝试添加的 intent.setFlags(...)的前的 startActivity(的)与各种组合致电 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_FROM_BACKGROUND ,没有运气。

在清单中的发射模式remoteControlActivity是 singleTask

在调试器中的 StartActivity()的被称为不落地的catch子句中,但我不打在RemoteControlActivity的onRestart或onResume处理断点。

在此先感谢!

编辑:的回答,下面,提出了不同的标志,所以我试了一下:

 尝试{
    意图I =新的意图(CTX,RemoteControlActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    ctx.startActivity(ⅰ);
}
赶上(ActivityNotFoundException E){
    Log.d(ShowButtons(正常),隐藏);
}
 

...但没有运气 - 在它调用startActivity调试,不会在catch块土地,但没有任何反应。

进一步修改:有人问我的清单;这里的一部分,主要活动:

 <活动机器人:launchMode =singleTask
    机器人:标签=@字符串/ APP_NAME
    机器人:windowNoTitle =假
    机器人:configChanges =方向
    机器人:screenOrientation =风景
    机器人:名称=RemoteControlActivity>
    <意向滤光器>
        <作用机器人:名称=android.intent.action.MAIN/>
        <类机器人:名称=android.intent.category.LAUNCHER/>
    &所述; /意图滤光器>
< /活性GT;
 

解决方案

标记 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT 只能由机器人设定时,它带来了一个活动,以正面本身。设置你自己什么都不做。

标记 Intent.FLAG_FROM_BACKGROUND 并没有做任何事情,它仅用于参考(以表示该活动是由一个后台任务开始)。

您需要设置 Intent.FLAG_ACTIVITY_NEW_TASK 。从该文件 FLAG_ACTIVITY_NEW_TASK

  

在使用此标志,如果一个任务的活动已经运行   你现在开始,那么一个新的活动将不会启动;   相反,当前的任务将简单地被带到的前   屏幕的状态是上次在

This question is a spin-off from a suggestion made in How to close or stop an external app that I started in Android.

I'm writing an Android app which is a remote-control for an industrial process - the process runs on a PC which is in constant communication with my Android app, Occasionally the PC sends a PDF file to the Android and I launch the AdobeReader.apk to display it. When the PC dismisses the image I want to dismiss it on the Android.

In the link above I was told that once I launch the AdobeReader there's no way to shut it down from my code. However I might be able to bring my app back to the front, which is just as good for my purposes. But I haven't been able to get it to work. The main activity for my app is RemoteControlActivity and I tried:

try {
   Intent i = new Intent(ctx, RemoteControlActivity.class);
   ctx.startActivity(i);    
}   
   catch (ActivityNotFoundException e)   {
   Log.d("ShowButtons(normal)", "Hide");
}

I also tried adding an intent.setFlags(...) before the startActivity() call with various combinations of Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_FROM_BACKGROUND with no luck.

In the manifest the launch mode for remoteControlActivity is singleTask

In the debugger the StartActivity() is called without landing in the Catch clause but I don't hit a breakpoint in RemoteControlActivity's onRestart or onResume handlers.

Thanks in advance!

EDIT: An answer, below, suggested a different flag so I tried it:

try {
    Intent i = new Intent(ctx, RemoteControlActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
    ctx.startActivity(i);          
}   
catch (ActivityNotFoundException e) {
    Log.d("ShowButtons(normal)", "Hide");
}

... but no luck - in the debugger it calls startActivity, does not land in the catch block, but nothing happens.

Further Edit: I was asked for the Manifest; here's the part for the main Activity:

<activity android:launchMode="singleTask"
    android:label="@string/app_name"
    android:windowNoTitle="false"
    android:configChanges="orientation"
    android:screenOrientation="landscape"
    android:name="RemoteControlActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />  
    </intent-filter>
</activity>

解决方案

Flag Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT is only set by Android when it brings an activity to the front itself. Setting it yourself does nothing.

Flag Intent.FLAG_FROM_BACKGROUND doesn't do anything, it is only used for informational purposes (to indicate that the activity was started by a background task).

You need to set Intent.FLAG_ACTIVITY_NEW_TASK. From the documentation for FLAG_ACTIVITY_NEW_TASK:

When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in.