添加退出按钮,Android应用程序从一个活动调用函数 [英] Add Exit button to Android App By Calling a Function from one Activity

查看:134
本文介绍了添加退出按钮,Android应用程序从一个活动调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个活动如下函数

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

推荐答案

作为一般的建议是:不要利用OS

其他然后在常规的桌面系统,机器人(和iOS)的,你通常没有你经常使用的的许多应用系统。由于大多数用户只使用一个完整的应用手很多时候,系统并没有完全杀死的应用程序,当您返回到主屏幕,使之更快地加载打开时的再次

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.

这将导致在一些应用中开始非常快(最常用的)。但是,Android系统需要占用的资源时,它会释放那些本身杀死一些旧的应用它们在后台仍在运行。

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!

如果您只是想添加返回从应用程序返回到主屏选项,调用的 this.finish() 活动。然后这将关闭当前的活动,并返回到主屏幕。

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.

随着最后一点,如果你希望你的应用程序的商店有关当前状态的信息(如看来你正在做使用共享preferences),你应该做你的活动的 的onStop() -method 的onPause() - 方法。请参阅文档以获取更多信息

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.

此方法将通过Android系统调用,不管你封闭自己或系统应用程序中关闭它(因为它需要的存储空间)。

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

和最后(和最低),某些常规的Andr​​oid编程意见

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.

活动的其实只是墙上画上。这意味着它们是显示的东西给用户。应用code(以及任何繁重的)应该总是被放入的AsyncTask 。这样一来,你的UI总是会反馈永不冻结,这将使用户感到紧张。

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.

这篇关于添加退出按钮,Android应用程序从一个活动调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆