Android exit app java

Java exit from app java android

After your advice i use this function Solution 1: If you want to exit why you are starting activity again. use only this.finish(), remove other statements. This might be helpful if you are sticking to what you have: how to clear the android stack of activities?

Exit from Application android

I tried some way to exit from application but each method just minimizes the app. I’d like to close down the app not minimize it. Exit Button is in MainAcitivity.

For now i’m calling this function, it works but not as i need.

I know this question is asked a lot but i can’t find solution exactly what i need.

After your advice i use this function

If you want to exit why you are starting activity again. use only this.finish(), remove other statements. Take look at activity life cycle for more info

Few things. In your code you can put finish(); after startActivity, this may help (did for me).

How it should be done however is the way the guidelines have it already. The back button does close the app. If you press the back button and then restart your app, you will be on the very first page activity, not where you left off.

In regards to the back button, you can Override it to control what it does if you are not happy with it. You can put your code in there and when the users presses the back button it will execute the code. Users are more accustomed to pressing the back button rather than a button within a layout (which I presume is what you may be doing).

This might be helpful if you are sticking to what you have: how to clear the android stack of activities?

Try with this single line code :

Читайте также:  Java передача ссылки на метод

You need to navigate to your main activity with Intent.FLAG_ACTIVITY_CLEAR_TOP, pass it some data that it will know that you are exiting the application. And just call finish from your main activity.

Another suggestion is Kamlesh Arya answer. How ever it is a bad practice as it will not destroy the activity stack, and next time the use lunch your application it will not start from the main activity but from the last active activity.

Java — How to exit an Android app programmatically?, But this will not kill the underlying activities in the same application. This will just minimize the application. If you want to exit from application use the following code to end its process: android.os.Process.killProcess(android.os.Process.myPid()); for mono development just use . process.KillProcess(Process.MyPid()); Code sampleandroid.os.Process.killProcess(android.os.Process.myPid());System.exit(1);>>);Feedback

Add Exit button to Android App By Calling a Function from one Activity

I have following function in one Activity

its working fine in its own Activity. and I want to call this another activity. I created class instance also as below

 Home hm = new Home(); hm.AppExit(); 
 06-29 19:41:06.024: E/AndroidRuntime(583): FATAL EXCEPTION: main 06-29 19:41:06.024: E/AndroidRuntime(583): java.lang.NullPointerException 06-29 19:41:06.024: E/AndroidRuntime(583): at android.app.Activity.startActivityForResult(Activity.java:2827) 06-29 19:41:06.024: E/AndroidRuntime(583): at android.app.Activity.startActivity(Activity.java:2933) 06-29 19:41:06.024: E/AndroidRuntime(583): at com.mythrii.timeApp.Home.AppExit(Home.java:219) 06-29 19:41:06.024: E/AndroidRuntime(583): at com.mythrii.timeApp.AdminPage.onOptionsItemSelected(AdminPage.java:205) 06-29 19:41:06.024: E/AndroidRuntime(583): at android.app.Activity.onMenuItemSelected(Activity.java:2205) 06-29 19:41:06.024: E/AndroidRuntime(583): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:748) 06-29 19:41:06.024: E/AndroidRuntime(583): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143) 06-29 19:41:06.024: E/AndroidRuntime(583): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855) 06-29 19:41:06.024: E/AndroidRuntime(583): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532) 06-29 19:41:06.024: E/AndroidRuntime(583): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122) 06-29 19:41:06.024: E/AndroidRuntime(583): at android.view.View$PerformClick.run(View.java:9080) 06-29 19:41:06.024: E/AndroidRuntime(583): at android.os.Handler.handleCallback(Handler.java:587) 06-29 19:41:06.024: E/AndroidRuntime(583): at android.os.Handler.dispatchMessage(Handler.java:92) 06-29 19:41:06.024: E/AndroidRuntime(583): at android.os.Looper.loop(Looper.java:130) 06-29 19:41:06.024: E/AndroidRuntime(583): at android.app.ActivityThread.main(ActivityThread.java:3683) 06-29 19:41:06.024: E/AndroidRuntime(583): at java.lang.reflect.Method.invokeNative(Native Method) 06-29 19:41:06.024: E/AndroidRuntime(583): at java.lang.reflect.Method.invoke(Method.java:507) 06-29 19:41:06.024: E/AndroidRuntime(583): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 06-29 19:41:06.024: E/AndroidRuntime(583): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 06-29 19:41:06.024: E/AndroidRuntime(583): at dalvik.system.NativeStart.main(Native Method) 

As a general advice: Don’t exploit the OS.

Other then in regular desktop systems, Android (and iOS) are systems where you normally don’t have that many applications you commonly use. Since most users only use a hand full of applications very often, the system does not completely «kill» the app when you return to the home-screen, to make it load up faster when it is opened again .

This will result in a few applications starting very fast (the most commonly used ones). But, when the Android system needs the occupied resources, it will free those by itself, killing off some older applications which are still running in the background.

Читайте также:  Как добавить font awesome в css

That’s why you normally shouldn’t terminate an application yourself.

Apart from the above statement, it seems that you’re trying to manually launch the home-screen. There is no need to do that!

If you simply want to add an option which returns from your application back to the home-screen, call this.finish() in your Activity . This will then close the current Activity and return to the home screen.

As the last point, if you want your application to store information about the current state (like it seems you’re doing using the Shared Preferences), you should do this in your Activity’s onStop() -method or onPause() -method. See the Docs for more information.

This method will be called by the Android system, regardless if you close your application yourself or the system closes it (because it needs memory space).

And last (and least), some general Android programming advices :

Don’t manually create activity’s with the new -operator. Let the system do it and use Intent s for that purpose.

Activity’s are really just «walls to paint on». They are meant to show something to the user. Application code (and any kind of heavy lifting) should almost always be put into an AsyncTask . This way, your UI will always respond and never «freeze», which will make users nervous.

For activities communication you can use broadcast messaging.

  • This is the Android reference: http://developer.android.com/reference/android/content/BroadcastReceiver.html
  • This is a question about that un SO:
Intent i = new Intent(this,Home.class); Bundle data = new Bundle(); data.putString("0","0"); i.putExtras(data); startActivity(i); 

Java — How to exit from an android app?, What are you trying to accomplish with an in app exit? If you want the app to not save state after the user exits you can add this flag to the main activity in the …

Читайте также:  Встроенный метод сортировки python

Java exit app causes infinite loop

i have made a simple java app to print a text file then should terminate. I tried to use the return statemend but didn’t work at all. So i used the system.exit(0) but instead of exit the app, it goes in an infinite loop. this is my code: can someone tell me what is wrong ?

package com.example.testprinterdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import com.pda3505.Service.CaptureService; import com.pda3505.printer.PrinterClassSerialPort; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class MainActivity extends Activity < public static PrinterClassSerialPort printerClass = null; protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); printerClass.write(new byte[] < 0x1d, 0x21,0x00,0x00>); printerClass.write(new byte[] < 0x1b, 0x26, 0x00 >); BufferedReader reader = null; try catch (FileNotFoundException e) String line = null; try catch (IOException e) while(line!=null) < printerClass.printText(line); printerClass.printText("\n"); try catch (IOException e) > system.exit(0); > private void init() < printerClass = new PrinterClassSerialPort(new Handler()); printerClass.open(this); Intent newIntent = new Intent(MainActivity.this, CaptureService.class); newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startService(newIntent); >> 
 try < //never hard code the file path in Android. String filePath = Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"temp.txt"; BufferedReader reader = new BufferedReader(new FileReader(filePath)); while (true)< String line = reader.readLine(); if(line.length() >0)< printerClass.printText(line); printerClass.printText("\n"); >else < break; //you can use return too >> >catch (IOException e)

In your approach, line is never null, it is either «» or it is » » or «\n» etc, but never null. so listen for length and break the loop

Please avoid multiple try catch blocks when you can have your code under single try block. And please scope the variables properly, do not declare variables outside the scope where it is not used, as it causes problems for JAVA Garbage collector/

Java — Exit from Application android, I tried some way to exit from application but each method just minimizes the app. I’d like to close down the app not minimize it. Exit Button is in …

Источник

Оцените статью