Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
644 views
in Technique[技术] by (71.8m points)

android - Bring my Activity back to the top?

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...