Android clear activity from memory - android

In my app I have a SIP calling feature.When the app is not opened and it is cleared from the system memory and it receives incoming call it opens the call activity from a SIP service. When the call is disconnected, I finish the activity.But the issue is, the activity is still in the memory (when home button is pressed).I want that activity to not be in the memory.Like whats app call behavior that don't keep activity in the memory when the app was killed i.e., not in the memory.
From SIP service I call it like this.
Intent intentIncomingCall = new Intent(getApplicationContext(), CallActivity.class);
intentIncomingCall.putExtra("DisplayName", callerDisplayName);
intentIncomingCall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentIncomingCall);
and on call disconnect, I just simple stop the service and call finish() on activity.
I have tried adding flags and exit(), things but nothing works.

android:allowTaskReparenting="false"
android:excludeFromRecents="true"
android:launchMode="singleTask"
android:permission="android.permission.USE_SIP"
android:screenOrientation="portrait"
android:taskAffinity=""
Try adding above properties to activity in manifest

Add line below finish() method :
finish();
System.exit(0);

when callClosed callback,
System.exit(0);
or
android.os.Process.killProcess(android.os.Process.myPid());

Related

Prevent a new intent or switching apps from destroying a activity

I have a activity where I do nothing with the following function (onPause/onStop/onDestroy). It is just how it was when you create a activity from scratch.
Now when I switch to a new app or I start a new activity with a new intent the activity is destroyed. The android system calls onPause -> onStop -> onDestroy.
How can I prevent the app from calling onDestory on that specific activity?
Code for starting the new activity:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
getApplicationContext().startActivity(intent);
Manifest code:
<activity android:name=".WebviewActivity">
</activity>
As you can see I have nothing in there.
Make sure you didn't accidentally enabled "don't keep activities" mode in developer options:
In general you can't, these are life cycles method called by Android environment, and you donot have any control over it. The Android runtime decides when to call these methods.
You are launching activity via Implicit call , so the android runtime determines the best suitable activity for it, and that activity may run in its own stack, which may cause calling of onDestroy on your activity, as its no more visible.
Ankit

Finish app from any activity [duplicate]

This question already has answers here:
Finish all activities at a time
(21 answers)
Closed 3 years ago.
I want to kill my app from a exit button on my navigation drawer. the drawer has a exit button that is suppose to just kill the whole app (all activity) and gets the user out of the app .
When i am using finish() in the first activity its working but when i go into the app and call finish() the activity gets killed and returns to previous activity but never really kills the app.
Tried to use System.exit(0) but no luck.
Extra Information Might Help
I have all my activity started with android:launchMode="singleTop" . it means all those activities that are already created will not be created again and just reordered to front on calling.
Do anyone have any suggestion for this , please do help.
Update
I want to make some updates here as my question looks like this SO Question.
As i have already said i am using a android:launchMode="singleTop" . and this answer is not working for my case. I have to call onCreate() to make this happen but it is not my case.
Use below code
public void AppExit()
{
this.finish();
Intent intent= new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
System.exit(0);
}
Call above function to exit from App.
First, you need to decide if you just need to take the user out of the app, or if you also need to finish() all of your activities. It depends on your needs, but my guess is that in most cases most users won't know the difference, and Android is designed to have many apps having activities still running. If this is all you need, then you can just use an Intent to take the user to the home screen.
Intent intent = new Intent(Intent.
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
If you actually want to finish() your activities, then you need a mechanism to inform all currently running activities that the user is exiting the app. You can use something like EventBus to send an event that will be received by all registered subscribers; if you don't want to use a third-party library, you can do the same type of thing with LocalBroadcastManager. To do that, you would register a BroadcastReceiver with an IntentFilter for a custom action and broadcast an Intent with that action when the user wants to exit.
In either case, every activity should receive the signal and call finish() on itself. Note that the subscription (in the EventBus case) or the registration of a receiver (in the LocalBroadcastManager case) must be still active after onStop(), so I would register in onCreate() and unregister in onDestroy().
Use below code in your exit button
Intent intent = new Intent(this, ActivityOne.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
And, in your ActivityOne.class oncreate() method just put below code
if (getIntent().getBooleanExtra("EXIT", false))
{
finish();
}
As a few answers say, call finish(); after you start an intent from an activity. That will make sure the current activity is the only running activity. Also try super.finish(), which will call finish() at parent activity.
You can alternatively use only one activity with many fragments. That way, if you call finish() in the exit button OnClickListener code, you'll exit the app. This will also save a lot of coding since you will be defining the navigation drawer once.
First of all, Android apps are not intended to be killed other that by the system, which attempts to keep them in memory in case the user will return.
But, if you have to do it, try this:
android.os.Process.killProcess(android.os.Process.myPid());

More than one instance of activity due to intent

For an App I am developing, I override the back button to make it act like the home button so that the state of the main activity is preserved even when the app is exited. Now, I also send a notification to the user from time to time using a service. When this notification is pressed I want to open the main activity again. I noticed though that this creates a second instance of the app, which creates major problems. I am trying to make the main activity go to the front again, without calling oncreate again like so:
Intent to launch main activity again:
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
This doesn't work though. I still end up with two instances of my main activity. Does anybody know how to fix this?
By the way, I already have android:launchMode="singleInstance" in my manifest.
There's a way to force the OS to create only one instance of an activity and thats using the tag launchMode in the Manifest as shown below:
<activity android:name="YourActivity"
android:launchMode="singleInstance"/>
Hope this Helps...
Regards
Try adding this flag to the intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP), works for me.

Android: Resume from service to activity

I have an activity and one service. When i press the home button the activity paused and the service starts. I want, when i starts again my application to stops the service and my activity starts....Thanks.
Change your design.
There is no need to switch between playing the music in an Activity and in a Service. Instead you should keep playing in the Service, and continually update the Activity from there using for example BroadcastReceivers.
These broadcastreceivers should be registered in the activity's OnResume and unregistered in the activity's OnPause.
See Using a Service with MediaPlayer from the official android documentation
Stop the service in OnResume(). Start the service in OnPause().
If you don't want/can't change the design of your app, you must use this flag with your Intent in your service:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
If you don't want a new instance of your Activity, set launchMode for your Activity in the AndroidManifest file:
android:launchMode="singleTask"

Reopen activity directly after finishing a phone call

After I make a call with phone call intent and finish the call, the phone opens calls log instead of directly reopen the app activity which launches the call intent.
How do I skip going to the call log? After finishing the call I want the phone to come back to my app activity?
launch/reopen the previous activity and set flag Intent.FLAG_ACTIVITY_SINGLE_TOP to the intent.
if this flag set, the activity will not be launched if it is already running at the top of the history stack.
You can watch the Broadcast of android.intent.action.PHONE_STATE and for the extra EXTRA_STATE. If it is EXTRA_STATE_IDLE the call is finished and you can implement the logic to reopen your app
Tried this to reopen app but the app crash
Intent i = new Intent("android.intent.action.MAIN");
i.setComponent(new ComponentName("com.muaz", "HantarSMS") );
i.addCategory("android.intent.category.LAUNCHER");
startActivity(i);

Categories

Resources