Ram kiran Pachigolla
Ram kiran Pachigolla

Reputation: 21191

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

I have following function in one Activity

  public void AppExit()
  {
    Editor edit = preferences.edit();
    edit.putString("pref_code", "");
    edit.commit();

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

    int pid = android.os.Process.myPid();
    android.os.Process.killProcess(pid);

  }

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

But its getting error as

  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)

Can any body Help me...

Upvotes: 0

Views: 2649

Answers (3)

Jack Satriano
Jack Satriano

Reputation: 2029

Run this:

Intent i = new Intent(this,Home.class);
Bundle data = new Bundle();
data.putString("0","0");
i.putExtras(data);
startActivity(i);

and in the Home activity:

protected void onResume() {

Intent i = getIntent();
if (i.getExtras() != null) {
    //AppExit();  unneccessary
    finish();
}

Upvotes: 1

Lukas Knuth
Lukas Knuth

Reputation: 25755

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.

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 Intents 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.

Upvotes: 2

Stefano Ortisi
Stefano Ortisi

Reputation: 5336

For activities communication you can use broadcast messaging.

How to send and receive broadcast message

  • Another good tutorial:

http://mobisys.in/blog/2012/01/android-sending-receiving-broadcast-messages/

Upvotes: 1

Related Questions