kill the app if it is not in foreground - android

Is there any way to kill the app as soon as it goes to background. For example, if I press the home key and again start the app, it goes to same activity but what I want is that as soon as app goes to background, it should be killed or it should start from the first activity.

it is not "killing" your app perse, but this will give you the effect you are after.
make the intents used to start your second-Nth activity with the FLAG_NO_HISTORY. That way they will be finished as soon as they leave the screen.
Intent i = new Intent(this, SomeOtherActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i);
When your activity leaves the screen it will be finished, which will cause it to launch from the starting activity the next time it gets launched from the home screen.

You can catch the event when the user presses the home button as follows
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
finish();
}
return super.onKeyDown(keyCode, event);
}
You can also override the onBackPressed() method in your activity and call finish from there.

Related

Android Application Single Instance

I have one problem in notification click event.
for example there is a four activity in application A,B,C,D.
Currently Activity B is open and i get notification ,when i click on notification i want to open activity D. and its its working fine but problem is when i click back button from actvity D, its open activity B.
I tried Intent.FLAG_ACTIVITY_CLEAR_TOP , Intent.FLAG_ACTIVITY_NO_HISTORY ,Intent.FLAG_ACTIVITY_SINGLE_TOP etc but still unsuccessful finish all intent.
please help me ,Thank you in advance.....
i found the solution, add flag in new Intent Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
If I understood your problem correctly, I think you have to override the back button, in order to give it a custom behaviour. Something like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Go to previous activity, or some other place
}
return super.onKeyDown(keyCode, event);
}

Start application with second screen

I have a question regarding android activity.
I have my first loginscreen(MainActivity) and from there go to Secondscreen, now when I am pressing escape button from the secondscreen I want to close the application(or android home screen) and next time open the application I want to start application with secondscreen.
i tried this code on my second screen but it does not working
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ESCAPE)
{
//My logic here
return true;
}
return false;
}
First time when you use the application, Start with LoginScreen,
then call finish(). Also store a shared preference to know that, the
application is used atleast once(Logged in)
Since you finished the LoginScreen, when you press back button on SecondScreen, you will come to device home, not the LoginScreen.
Next time when you want to Start the application, check the sharedPreference whether the user is logged in, and finish the LoginScreen just at the start of onCreate
LoginScreenActivity:
onCreate(){
//check shared preference if logged in
if(yes){
startActivity(new Intent(Login.this, Second.class));
finish();
}else{
//do all the login here then,
//set sharedPreference
startActivity(new Intent(Login.this, Second.class));
finish();
}
}
What I assume you are trying something like below:
Frist installation#
LoginScreen>SecondScreen, when you press escape it will close entire app.
Second Time#
When you open app it will start from second screen instead loginScreen.
Solution
Phase1#
Store your login info in database or sharepreference when first time login has been made, and finish() loginscreen while opening second screen.
Phase2#
When you open second time your app, check your login info, if it is exists then redirect to second activity else open loginScreen.
When you start your second activity at that time finish the existing acitivity with finish()
example
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
finish();
This code will finish your current acitivity and when your press back button in second activity then the application gose close.
In log in activity call finish after starting second activity.
startActivity(i);
finish();
Now if u press back it should close the app and when u going to start app again the second activity will be visible.
There are two ways to do that:
make your log in activity with "noHistory=true" in android menifest activity definition.
<activity android:name="YOUR_FIRST_ACTIVITY"
android:label="FIRST_ACTIVITY"
android:noHistory="true" />
You can finish() the first activity at the time of starting second activity.
startActivity(SECOND_ACTIVITY_INTENT);
FIRST_ACTIVITY.this.finish();
Hope it will help you.

How to dismiss previous activities in android at a certain point?

I have activities A -> B -> C where A and B are for login and verification purpose. When a user reaches activity C, A and B are not necessary any more. That means if a user press BACK in C, the application should not go thru A and B, instead it should go back to HOME screen.
What's the conventional way to implement this in android? Thanks.
EDIT: to clarify a bit, user should be able to go back to A from B during login/verification phase, but not from C to A or B once the user reaches C.
When going to next activity call
finish();
before starting next activity.
Or, if not pressing back but going to next activity:
Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Using the following code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Intent data = new Intent(this, HomeScreenActivity.class);
data.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(data);
finish();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
This way you override the back key behaviour and start a selected activity removing all other activities in between from the stack.
The simplest is to add
android:nohistory="true"
in your manifest for Activity A and B.
Whether or not the activity should be removed from the activity stack and finished (its finish() method called) when the user navigates away from it and it's no longer visible on screen — "true" if it should be finished, and "false" if not. The default value is "false".
A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.

Notifications opening activities and the back button? Restricting to one instance?

There's a situation I have that's related to the Activity stack but I'm new enough to Android dev to not now how to respond to it.
I have an application that works like a stopwatch. When you start it, an ongoing notification goes in the notification tray and remains ongoing until you stop the stopwatch. I've noticed that if my stopwatch is running in the foreground, and I touch the notification, it creates a new instance of my stopwatch's activity and slides the old one off screen. This creates problems with the back button (you press back and the new instance goes away to reveal the old instance). It makes sense why this happens, but I don't want it to happen like this. What can I do to prevent multiple instances of my application from running?
What can I do to prevent multiple instances of my application from running?
In your Intent you use with the PendingIntent for the Notification, add setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); or setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);, whichever behavior fits your needs better.
You could also disable the back button. Not as elegant as CommonsWare solution but nothing wrong with some options!
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
return true;
}
return super.onKeyDown(keyCode, event);
}

Starting and stopping services

I have a service running (Socket), this is how i start the service.
Intent s = new Intent(this, Socket.class);
startService(s);
in every activity i check for the user to select the home button, as soon as the home button is clicked i need to destroy the socket, so i have the below code on every activity in my app:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_HOME)
{
Intent s = new Intent(this, Socket.class);
stopService(s);
}
return true;
}
but this doesn't seem to stop my service?
Am i missing something? I need to destroy my service as soon as the home button is clicked.
Instead of hooking into keypress events and such, perhaps working with the built-in events like onPause, onStart, onDestroy, etc is more suited to your needs?
Another question on StackOVF recently had a brilliant reply with a flowchart that can help you figure out where to start/stop any other stuff you're using:
http://developer.android.com/images/activity_lifecycle.png
creds to monoceres for posting that in this topic:
App crashes after receving phone call

Categories

Resources