Finish is not working - activity - android

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?

Related

Android: resume a singleTask activity

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.

Other activity is also brought to front on StartActivity

I have two activities (A and B) in my app and some BroadcastReceiver.
I encounter the following scenario:
A is running and was closed using the home button (onStop was called).
Some time after, BroadcastReceiver was triggered with some intent. It run the following code:
Intent activityIntent = new Intent(context,
B.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(activityIntent);
And B is indeed started, however A is also brought to front (behind B). How could I avoid A being fronted?
when you are pressing home button you are not closing the app actually(i mean it's in pause state) and it's in back stack and whenever you started a new activity of same app and close that activity it pops out the top activity in back stack.. So if you don't need this thing happen then please try following code
#Override
public void onStop() {
if(!isFinishing())
finish();
super.onStop();
}
From BoardcastReceiver you can start Activity B using this intent filter. This will clear the activity stack and pop Activity A from the stack.
Intent intent = new Intent(context,B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);

Closing an android application programatically in android

I have a application that has two activities
Activity1
Activity2
Now i start Activity1 and move to Activity2 now i want to close the application at a moment
If i use finish() i am able to close only Activity2 and not
Activity1
What i want to do ::
i want my application to quit(close all activities and go to
homescreen)
But i don't want my apk to be removed from android system itself
How can i achieve this ?
{Edit}
I used the code(Activity2 has a fragment which launched a dialog on
"ok" condition in positive condition i am performing this condition below)
I am able to launch the previous activity but it is not canceled, its
just reloaded
Intent intent = new Intent(getActivity(), SplashActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
getActivity().finish();
You need to make your Activity2 as "top task" with a Flag when you call it as follows:
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
See the Reference:
If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished.
Then, call finish() in your Activity2. This should do the trick.
There is a way to kill your application process, but it is not recommended
this.finish();
Process.killProcess(Process.myPid());
This will kill your application and releases all the memory associated with it.
Note: Assuming your app has a single process id
Do like this
Intent intent = new Intent(Activity1.this, Activity2.class);
startActivity(intent);
Activity1.this.finish();
Intent intObj=new Intent(this, Homescree.class);
intObj.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intObj);

How to remove all activity in Stack when press back button

I have a list of activities A - B -C -D - E and more, for example final activity is K. I want clear all these activities in stack when i press BACK button. How can i do ? In fact, i over ride
onBackPress(){
moveTaskToBack(true);
finish();
}
but only current activity is deleted and application exit. Then, i come back application, it resume activity before K. I want it start from begining when i re-open app. I think the reason here is because the list of activities in stack still are stored, so i want to clear all stack when clicking BACK button. Any suggestions ? thank you very much !
There is method called finishAffinity() for finishing all activity.
public void onBackPressed(){
super.onBackPressed();
this.finishAffinity();}
You need to call your activity with the FLAG_ACTIVITY_CLEAR_TOP inside your onBackPressed
#Override
public void onBackPressed()
{
Intent it = new Intent(YouCurrentActivity.this, YourFinalActivity.class);
it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(it);
finish();
}
Hope it Helps!
Either use the noHistory flag in the manifest or finish each activity yourself when the user navigates away.
startActivity(myIntent);
finish();
Another solution, maybe the best, if you have so many overlaying Activities: use only one Activity and handle the content in Fragments. This way you are in control what exactly you want to show when the user hits the back button.
In API level 11 or greater, use FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK flag on Intent to clear all the activity stack.
Add this code on your onBackPressed() method,
> This launch mode can also be used to
good effect in conjunction with
FLAG_ACTIVITY_NEW_TASK: if used to
start the root activity of a task, it
will bring any currently running
instance of that task to the
foreground, and then clear it to its
root state. This is especially useful,
for example, when launching an
activity from the notification
manager.
So your code to launch B would be:
Intent intent = new Intent(A.this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish(); // call this to finish the current activity

Activity with Intent.FLAG_ACTIVITY_NEW_TASK has been destroyed

I'm using this way to start activity from Service, if my app is background:
Intent intent = new Intent(this, FromBackgroundActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
it works perfect,
but if FromBackgroundActivity is active and I press Home key it puts all to background.
And if I click on my app in taskbar it restores my app to foreground but without FromBackgroundActivity, it is destroyed. (
how can i restore my app from background with FromBackgroundActivity?
Try intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
It'll clear all the task and go to the FromBackgroundActivity.

Categories

Resources