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 itsonCreate()
: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
Gridlayoutmanager - How to Auto Fit Columns
How to Create an Android Activity and Service That Use Separate Processes
How to Clear the Volley Cache Automatically
How to Scroll Recyclerview Programmatically
Android: Using HTML5 to Determine Geolocation in Webview with JavaScript API
Mysterious Stacktrace in Android Developer Console (Bitmap Size Exceeds 32Bits)
How to Tell If My Textview Has Been Ellipsized
Using Android Google Maps V2 with Custom View or Imageview
Flutter: How to Fix "A Renderflex Overflowed by Pixels " Error
Arrayindexoutofboundsexception with Custom Android Adapter for Multiple Views in Listview
How to Programmatically Include Layout in Android
Why Does Flag_Activity_Clear_Top Not Work
Mediaplayer Stop Playing After About 5 Seconds
How to Work-Around the Blank Title in Pagertitlestrip and Pagertabstrip
How to Override a Module/Dependency in a Unit Test with Dagger 2.0