Android: resume a singleTask activity - android

I have an activity with an UDP socket on a port. If I press the Home button the activity goes in background, OnPause() and OnStop() methods are called. Now I want to resume my activity when I receive some UDP packet.
Reading the other posts I understand I have to:
declare the activity as android:launchMode="singleTask" (or singleInstance)
Then, when I want to resume the activity:
Intent intent = new Intent(this.getApplicationContext(), myActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
This solution does not work form me. The call to startActivity(intent) does not show my activity on foreground and onResume() is not called.
The following flags do the job but I don't want to clear the task and restart a new one.
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

In order to bring your own task to the foreground, you need to call startActivity() on a non-Activity Context, for example:
getApplicationContext().startActivity(intent);
Also, you don't need to use any special launch mode (singleTask or singleInstance) for this to work.
This is an Android bug that was fixed in Android 4.4. Since this question is from 2014, I will assume that OP was seeing this problem.
See How to resume Android Activity programmatically from background and the comment thread.

Related

How to close all activity before launch new activity from AppWidgetProvider android

Hi I want to close all activity before launch new activity from AppWidgetProvider. I'm not having idea to do this. could you please suggest me any idea to do this?
you can use finishAffinity() method (from Activity, doc here). launch dummy invisible/transparent Activity, finishAffinity all Activityies and startActivity with proper Intent
there are also some flags for Intents which should clear your Activity backstack, but it depends on declared launchMode in Manifest. you may check these
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
or
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP);

Finish is not working - activity

I want to finish my activity, tried:
finish();
Activity.this.finish();
finishAffinity();
killing process...
It's how I am opening activity from service:
Intent a = new Intent(getApplicationContext(), Axctivity.class);
a.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(a);
stopSelf();
After finish, activity is succesfully hidden but still stay on runnining apps list when I can restore it.
It's how finish() works. It doesn't destroy the object nor the activity.
The android OS keeps the processes around to speed up getting back into the applications.
See what exactly Activity.finish() method is doing?

How to clear the activity stack and bring a running activity to foreground in Android?

I am starting activities(one or more) of another android application from one of the activities(Activity A) of my application to run some test scripts. After running, I want Activity A to be brought to the foreground the activities of the other application to be closed.
I am trying to achieve this by setting the launchMode of Activity A as singleTop in the manifest and using this snippet:
Intent intent = new Intent(getApplication(), ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_CLEAR_TOP); //Clear Top Flag
startActivity(intent);
I have also tried using Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK when launching ActivityA which will clear the entire task and create a new instance of ActivityA whereas I want the running instance of ActivityA to be brought to foreground
I have also tried to implement code samples from this link
Thanks in advance
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
This will bring the specified activity to top of the stack and clear rest of them.

Make PendingIntent from notification not fire if the Activity is already on front

Service A creates a Notification with a PendingIntent that opens Activity B. User pulls the notification drawer down and presses the Notification with Activity B on front previously. The PendingIntent basically adds another instance of Activity B instead of opening the currently open one.
Is there any way to make the PendingIntent open the Activity if it's already open, go back on the backstack if there's an instance of that Activity there, and open a new instance otherwise?
Add Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP when creating the Intent for your Notification.
Setting both Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_SINGLE_TOP will cause an existing instance of the Activity to be used (onNewIntent() will be called). If you only specify Intent.FLAG_ACTIVITY_CLEAR_TOP and there is already an existing instance of the Activity, that instance (and all other activities on top of it) will be finished and removed from the stack and a new instance of the Activity will be created (onCreate() will be called).
Yes, you can use the single task or single instance launch mode of your activity (or in your pending intent flags) to control this behavior.
Check the launch modes documentation for details on how to set those flags.
You can set Activity's attribute in Manifest file as following:
android:launchMode="singleTask"
or
On defining Flags for PendingIntent, you can use flags as:
Intent in=new Intent();
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);

Resume activity from broadcast receiver

I'm trying to resume an activity from within a broadcast receiver's onReceive() method as follows:
Intent i = new Intent(context, TimerSet.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
However the activity (TimerSet.class) is recreated instead of resumed. The only recommended solution I found to this problem was to use the FLAG_ACTIVITY_REORDER_TO_FRONT but I'm already using it.
Also, using Intent.FLAG_ACTIVITY_NEW_TASK doesn't fit my use case but I get the following exception when I do not provide it:
android:util.AndroidRuntimeException: Calling startActivity() from outside of an
Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you
want?
I am not sure whether this is exactly your problem or not, but I have a situation where I got a notification and I want to start my app without starting a new instance (if it's already running)
I finally figured out that these will work. The FLAG_ACTIVITY_NEW_TASK will not start a new instant if the activity has already been running. However, it will add it to the existing stack. Therefore, we can do a FLAG_ACTIVITY_CLEAR_TOP, so back will bring user to the home screen but not the previous state.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
remove FLAG_ACTIVITY_NEW_TASK flag.
also add this flag ->FLAG_ACTIVITY_CLEAR_TOP. This would prevent new activity to be created if already present.

Categories

Resources