How to Bring a Background Task to The Front Below Honeycomb

Bring task to front on widget click

Just start the main (root) activity of your application like this:

Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

If the activity is already running in a task, this will just bring that task to the foreground (without creating any new instances of the activity). If the activity is not already running, then this will start a new task with that activity at the root.

Bring an Activity to the front using Android TaskManager

Make sure you are not using FLAG_NEW_TASK when launching Activity 2.

Bring task to front on android.intent.action.USER_PRESENT

Here's a slightly hackish implementation that works for me:

  • Create a simple BringToFront activity that simply finish() itself on its onCreate():

    public class BringToFront extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    finish();
    }

    }
  • In your BroadcastReceiver, start the BringToFront activity above instead of your activity_A if the action is USER_PRESENT:

    @Override
    public void onReceive(Context context, Intent intent) {
    Class<? extends Activity> activityClass = activity_A.class;

    if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
    activityClass = BringToFront.class;
    }

    Intent i = new Intent(context, activityClass);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
    }

This works because the BringToFront activity has the same taskAffinity as your activity_A and starting it will make the system bring the existing task to the foreground. The BringToFront activity then immediately exit, bringing the last activity on your task (activity_B in your scenario) to the front.

It's worth noting that on API level 11 (Honeycomb), a moveTaskToFront() method is added to the system's ActivityManager service that might probably be the better way to achieve what you want.

How to bring android paused activity to front

I found the following solution that seems to work. It is a bit convoluted but does what I want.

See: Bring task to front on android.intent.action.USER_PRESENT

I made the following method to call the BringToFront activity that simple calls finish();

private void bringToFront()
{
Intent i = new Intent(getBaseContext(), BringToFront.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getBaseContext().startActivity(i);
}

It seems to me that there should be a simpler way to bring a task to the front. Will leave open for a bit to see if a better solution is suggested.

move app from background to foreground using its package name and `moveTaskToFront()` method in android

    ActivityManager am = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
List<RunningTaskInfo> rt = am.getRunningTasks(Integer.MAX_VALUE);

for (int i = 0; i < rt.size(); i++)
{
// bring to front
if (rt.get(i).baseActivity.toShortString().indexOf("yourproject") > -1) {
am.moveTaskToFront(rt.get(i).id, ActivityManager.MOVE_TASK_WITH_HOME);
}
}

Inside your manifest add :

<!--User Permissions-->
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.REORDER_TASKS" />

Whats the best way to bring an already running app to the front in android?

I have not done this myself.But i think you can achieve this using android:launchMode giving something like singleTop .here the reference link



Related Topics



Leave a reply



Submit