Android Application Single Instance - android

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);
}

Related

kill the app if it is not in foreground

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.

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.

Going Two Activities back?

I have this Activities sequence:
Avtivity01 => Avtivity02 => Avtivity03
You can go from Avtivity01 to Avtivity02 after you click a button.
You can go from Avtivity02 to Avtivity03 after you click a button.
-
I want to go from Activity03 to Activity01 DIRECTLY after I click a button.
-
NOTE:
I do NOT want to use Intent, because I want to have Activity01 as if I pressed the back button from Activity02
How to do that, please?
Why can't you use an Intent? You can use the FLAG_ACTIVITY_CLEAR_TOP when you click the button. Edit: If you want to preserve the original instance of the Activity, you can use this flag in conjunction with FLAG_ACTIVITY_SINGLE_TOP.
Do you ever want to be able to press a button from Activity03 to go back to Activity02? If you ALWAYS want it to go back to Activity01, you could alternatively use android:noHistory="true" on Activity02 in the manifest and just call finish() on Activity03.
You can go back more than one screen to which ever screen you need using intent and use flags to prevent going back to same screen :
Intent gotoScreenVar = new Intent(goFromScreenCls.this, goToScreenCls.class);
gotoScreenVar.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(gotoScreenVar);
I was had the same problem,
Short answer is add this line to intent.
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Long answer is:
I have MainActivity, SecondActivity and ThirdActivity what i want to do is opening SecondActivity from MainActivity and then ThirdActivity from SecondActivity and i want to click a button in the ThirdActivity that close both of SecondActivity and ThirdActivity without calling a new MainActivity i want it to functional as i back to it not creating a new one And the most thing i need is to keep back button functional normal in the ThirdActivity to back to SecondActivity if i need to change some of my choices in the SecondActivity so i can't call finish(); in the SecondActivity after starting ThirdActivity or any other solutions like android:noHistory="true" in the SecondActivity manifest because those solutions kill the SecondActivity and i can't go back to it throw back button.
so this flag solve the problem for me FLAG_ACTIVITY_REORDER_TO_FRONT
FLAG_ACTIVITY_REORDER_TO_FRONT
If set in an Intent passed to Context.startActivity(), this flag will cause the >launched activity to be brought to the front of its task's history stack if it >is already running.
go to reference here
so i tried this code to go back my MainActivity from ThirdActivity when i click the button in ThirdActivity . i hope this will help you.
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
link has good article. This will help you. By the way you can use FLAG_ACTIVITY_REORDER_TO_FRONT to solve your problem.
You can override Back button click on Activity3
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
startActivity(Activity3.this,Activity1.class);
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
It will work with just one line.
after calling your third activity call finish() to next line. and it will work.
eg.
In your Activity02
Intent thirdActivity = new Intent(this,Activity03.class);
startActivity(thirdActivity);
finish();

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