Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bring my Activity back to the top?

Tags:

android

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>
like image 704
user316117 Avatar asked Jul 12 '12 14:07

user316117


People also ask

How do you bring an activity to the foreground?

This example demonstrates how do I bring an activity to the foreground (top of stack) in android. Step 1 −Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I go back into activity?

In the second activity, the back button at the top left can be used to go back to the previous activity.


1 Answers

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.

like image 173
David Wasser Avatar answered Sep 21 '22 23:09

David Wasser