I have an app that when it launches, starts the main activity, all its okay, this activity sends and listen to info from an XMPP Server. If I change activity this send/read doesn't stop but when I go back to main activity, ** I reconnect to Server. Its okay but the Server read Disconnect-Connect when I go back to main activity from any other activity.
My question is, how I can launch the main activity from other without Disconnect-Connect issue (?)** I think maybe exists a method like startActivity but without restart It (I don't use finish)... Only go back to activity showing it, but never stop or start It... Something like show(enable/disable) . The activity works and the second plane and never stop/restart. This activity has a lot of threads that cant be rebooting all time.
Why dont you make you client-server communication in Application class, so you always have the same instance, and you will be able to manually control lifecycle of communication?
Related
I am trying to create an app that will keep my device wake until I tell it to turn off via code. I can use the follow code in a application...
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
That will work until I tap on the home soft button and then the flags will clear. So, I thought of putting that code in a foreground notification service and broadcast receiver for my action buttons. I can't getWindow in a service or broadcast receiver classes unfortunately. Is there an easy way to get around this?
My goal is to setFlags to keep the screen on from the Activity, and then the service takes over from there so I can close the app, and the keep on screen stays on until the service is turned off.
Basically I am trying to make an activity containing a button which reboots the device and after the reboot returns to the same activity.
I understand that this question may get confused with others about rebooting the device, but that is not the focus of this question as I can get the device to reboot fine.
I have made the button reboot the device but the only way I can get it to start the activity after it's finished rebooting is to register a broadcast receiver for BOOT_COMPLETED in the manifest. The trouble is that this method starts the activity every time the device boots which is undesirable. When I register the receiver on the button click listener it does not start the activity after the reboot.
I was wondering if there might be an extra in BOOT_COMPLETED that I could use to decide if it had been purposefully rebooted.
Any advice would be appreciated, thanks in advance!
Just save an integer corresponding to device purposely being rebooted through your activity. Use SharedPreference for the same. On reboot, in your broadcast receiver, check if the value is set. If it is set, start your activity, otherwise, let it go.
EDIT :
Always, unset this value when reboot is complete and your Activity is in front.
Your XML should be stored in a file named AndroidManifest.xml, not manifest.java.
Another reason your code is not being run, might be that your App is installed on external storage (sdcard). BOOT_COMPLETE is sent to applications before external storage is mounted. So if application is installed to external storage it won't receive BOOT_COMPLETE broadcast message.
If that isn't the problem, there is already a very good description of how to get boot completed receivers working on Android.
Trying to start a service on boot on Android
I'm new at developing Android. I'm experiencing the following problem:
I'm working on a Android application that extends the Application class. The values for the authentication session are stored in this class, because it is supposed to get created once and it is available in all activitys and services.
When I open my application and close it with the home button, and reopen it after I opened a lot of other apps, the base application class gets recreated (onCreate gets called). When this happens, I lose my authentication values. It seems like it that the onTerminate or onLowMemory are not getting called before the onCreate is getting called again. The current state of my application is not gone, because the previously active activity is still available.
When I close the app with the home button and only open a few other apps, the application resumes normally.
I also tried to implement a singleton class, but the singleton object is also null if the application class gets recreated.
How can I prevent the recreation of the application class, or work around this problem?
Thanks
Jacob
I found out that the application class can also be restarted as part of the application lifecycle. I stored my authentication values in preferences to solve this problem
From documentation of Application.onTerminate():
This method is for use in emulated process environments. It will never
be called on a production Android device, where processes are removed
by simply killing them; no user code (including this callback) is
executed when doing so.
Also there is no guarantee that onLowMemory will be called. Check this answer.
The current state of my application is not gone, because the
previously active activity is still available.
I think activity is just restored from Bundle.
How can I prevent the recreation of the application class, or work
around this problem?
You have to be ready that you application can be recreted in any moment. You can store some data in this class but before grab this data from some activity you should check if it is null. It will be null in first start and after application recreating. Just fill it with your data and return to activity.
I have an activity which can be launched from two sources. Each source launches it's own instance of the activity:
1. From inside an application
2. From an external service
My problem is with the service. The service needs to be able to control the visibility of the activity on the Android screen (hide and show the activity).
Currently I start the activity from the service with the following intent:
Intent appIntent = new Intent("Custom_Action");
appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
startActivity(appIntent);
My question is, how can I control the visibility of the activity. Maybe through binding the activity to the service?
I think you're misunderstanding how services and activities relate. A service runs in the background, doing it's thing. It has no UI and no way for the user to interact with it. The activity can send the service messages and query its state but the service has no way to push information to an activity. If you want the service to notify the user of something you can post a notification. Then have that notification launch your activity which will then connect to the service. There is no way to force open an activity from a service because there is no way to know if your user is even looking at their device at that time.
My android application allows to launch other installed applications from this.This shows some allowed apps. If the user try to launch a disallowed application then show a message and go back to my activity (from where each application launch) using running tasks. My application act as a home launcher.So intent to this activity if the is a blocked application.For eg: It is possible to launch Camera from Gallery in Samsung device.If the camera is not an allowed one shows blocked message and exited to my acivity.But when relaunching Gallery the blocked message shows again beause the top activity(Camera activity) lied in the stack.
But the exiting of these blocked application did not work perfectly.
Is it possible to get close/exit event of an application?
How can i finish an application as whole(By finishing all its applications).
How to launch an application without having any history of previous launch?
Thanks in Advance
Is it possible to get close/exit event of an application?
Yes it is possible inside your LauncherActivity You can override onDestroy this method will be called on application exit.
How can i finish an application as whole(By finishing all its applications)?
I believe you want to stop your all running activities here. This can be achieved in multiple ways.
android.os.Process.killProcess(android.os.Process.myPid());
or
Intent intent = new Intent(getApplicationContext(), YourHomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will clear all the activities and will brings the user to the HomeActivity. While calling this you can add a flag in intent and using that value you can finish the HomeActivity also. Use finish() method to finish the activity.
How to launch an application without having any history of previous
launch?
You can use the same above Solution to achieve this. The second one.
Hope this will help.
There is an onTerminate method in application class, but this cannot be used in production environment. See more about this here