简体   繁体   English

Android 如何在“强制停止”应用程序后获取最后的焦点活动

[英]Android How to get Last Focused Activity after "Force Stop" the Application

I am developing android Application having number of activities.我正在开发 android 具有多个活动的应用程序。 The requirement of the application is When i "FORCESTOP" the application manually(Settings->Applications->ManageApplication->APP_NAME->FORCESTOP), the last focused activity(UI Page) to be displayed.应用程序的要求是当我手动“FORCESTOP”应用程序(设置->应用程序->管理应用程序->APP_NAME->FORCESTOP)时,要显示的最后一个焦点活动(UI 页面)。 Can i get the Last focused Activity after *FORCESTOP *ing the Application?我可以在*FORCESTOP *ing 应用程序后获得最后的焦点活动吗? Please help me with the sample code.请帮助我提供示例代码。

You should keep track of each activity change and save it to file (eg in your sharedpreferences) immediately when the change happens.您应该跟踪每个活动更改,并在更改发生时立即将其保存到文件中(例如,在您的共享首选项中)。 Something like this像这样的东西

protected void onResume() {
    SharedPreferences.Editor edit =  PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()).edit();
    edit.putString("LastActiveActivity", this.getClass().getName());
    edit.commit();
    super.onResume();
}

Make sure you save the current activity in the onResume() method of all your activities.确保将当前活动保存在所有活动的onResume()方法中。

When you restart you can read the last activity back from your preferences.当您重新启动时,您可以从您的首选项中读取上次活动。 This should be done in the onCreate() method of your main activity (the one that is specified as "LAUNCHER" in your manifest).这应该在您的主要活动(在您的清单中指定为“LAUNCHER”的活动)的onCreate()方法中完成。 Then start the activity with the name you've read back.然后用你读过的名字开始活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM