Android使用共享的首选项和Dispatcher活动,以返回到上一个活动(重启手机后) [英] Android using shared preference and Dispatcher activity, to get back to the last activity (after reboot the phone)
问题描述
我正在使用共享首选项来保存应用程序的状态,以便能够在重新启动Android手机之前进入上一个活动.我的目的是当Android系统管理内存并推出我的应用程序时,当用户重新进入应用程序时,他们将进入我应用程序的最后一个活动和屏幕.
I'm using a Shared Preferences to save the state of my application to be able to get to the last activity before my Android Handphone got re-boot-ed. My purpose is when Android system manage the memory and push out my apps, when users re-enter to the apps, they will get to the last Activity and screen of my apps.
这是我的一些代码:
首先是DispatcherActivity.java:
First is DispatcherActivity.java :
> package com.lm.rosary;
>
> import android.app.Activity; import android.content.Intent; import
> android.content.SharedPreferences; import android.os.Bundle;
>
>
> public class DispatcherActivity extends Activity {
>
> @Override
> protected void onCreate(Bundle savedInstanceState) {
> super.onCreate(savedInstanceState);
>
> Class<?> activityClass;
>
> try {
> SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
> activityClass = Class.forName(
> prefs.getString("lastActivity", Jfp1.class.getName()));
> } catch(ClassNotFoundException ex) {
> activityClass = Jfp1.class;
> }
>
> startActivity(new Intent(this, activityClass));
> } }
第二个是我的示例活动(Jfp1.java),我希望用户在重新启动或重新启动电话时回到此屏幕. Jfp1.java:
Second is my sample activity ( Jfp1.java ) that I want users to get back to this screen when phone re-boot or re-started. Jfp1.java :
> import android.content.SharedPreferences; import
> android.content.SharedPreferences.Editor;
>
>
> public class Jfp1 extends Activity implements OnClickListener { ....
>
> SharedPreferences prefs;
> SharedPreferences.Editor editor;
> .... @Override protected void onPause() { super.onPause();
> SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
> Editor editor = prefs.edit();
> editor.putString("lastActivity", getClass().getName());
> editor.commit(); }
我的应用程序,从MainActivity到Mysterytopray,再到Jfp1类. DispatcherActivity.java在单独的类上.
My apps, start from MainActivity to Mysterytopray and to Jfp1 class. DispatcherActivity.java is on separate class.
然后像往常一样将我的清单第三个:
And third my Manifest as usual :
> <activity
> android:name=".MainActivity"
> android:label="@string/app_name" >
> <intent-filter>
> <action android:name="android.intent.action.MAIN" />
>
> <category android:name="android.intent.category.LAUNCHER" />
> </intent-filter>
> </activity>
>
> <activity
> android:name="com.lm.rosary.DispatcherActivity"
> android:label="@string/app_name" >
> <intent-filter>
> <action android:name="android.intent.action.MAIN" />
> </intent-filter>
> </activity>
>
> <activity
> android:name="com.lm.rosary.Jfp1"
> android:label="@string/app_name" >
> </activity>
我在Jfp1屏幕上,然后重新启动了HP.但是,在HP重新启动后,我单击了我的应用程序图标,但没有首先显示Jfp1活动.主要活动排在第一位.但实际上,我希望它返回到上一个活动Jfp1.java.
I'm on the Jfp1 screen, and I reboot the HP. But after HP restarted, I'm clicking my apps icon, but Jfp1 activity is not appeared first. Main Activity that come first. But actually, I want it to get back to the last Activity that is Jfp1.java.
我没有使用Android的经验,因此任何人都可以给我一些建议并更正我的编码.非常感谢.
I'm not experienced on Android, so anyone please give me some advice and correct my coding. Thanks a lot.
推荐答案
使用此新代码
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.lm.rosary.DispatcherActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.lm.rosary.Jfp1"
android:label="@string/app_name" >
</activity>
更新: 调用finish()更合适,在
UPDATE: Call finish() is much more appropriate, add this below
startActivity(new Intent(this, activityClass));
finish();
这篇关于Android使用共享的首选项和Dispatcher活动,以返回到上一个活动(重启手机后)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!