How to bring a background task to the front below

2019-01-08 00:54发布

问题:

in my applications root activity, I have a customized tab bar, containing three tabs to switch between three screens implemented using ViewFlipper.

What I now want to do is to give each of those screens it's own activity stack, making the tab bar available in all of my activities, and switching between the different stacks when pressing a tab.

Works like a charme on honeycomb, where I have bringTaskToFront() available in ActivityMaganger class.

I tried many solutions, e.g.:

  • restarting the top activity of the specific task in the hope that the task will move to front
  • getting the honeycomb classes in somehow

So, any idea ?

Many thanks,

Sven

回答1:

In versions honeycomb and up, you can use MoveTaskToFront like so

//Bring Task to front with Android >= Honeycomb
if (Build.VERSION.SDK_INT >= 11) {
    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);
           }
    }
}

Make sure your Project Compiles with Android v.11 or up sothat this line doesn't break with compile:

am.moveTaskToFront(rt.get(i).id, ActivityManager.MOVE_TASK_WITH_HOME);

For older Android versions you can use the following to move the task to the front:

Intent intent = new Intent(this, YourClass.class);//The class you want to show
startActivity(intent);

In the android manifest add the following

<!--The versions with which to compile-->
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="11"/>

<!--The Activity you wish to bring forward-->
<activity android:name="YourClass" android:taskAffinity="" android:launchMode="singleTask" />

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

And then lastly, Make sure when you push the task to back by overriding the onBackPressed(), DO NOT USE moveTaskToBack(), It will result in your task not ending up visible to the user. Rather use the following: This will simulate a home button press which will effectively hide your application and show the home screen

@Override
public void onBackPressed() {
    Utils.showToast(this, "Your application is running in the background");
    /*Simulate Home Button Press*/
    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(startMain);
}

And there you go, I hope this works for you and helps a few people with this problem.



回答2:

This works for me on android versions above and below Honeycomb. The keys are the intent flags

Intent intent = new Intent(context, YourClass.class);//The class you want to show

intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(intent);


标签: android task