简体   繁体   中英

Android using shared preference and Dispatcher activity, to get back to the last activity (after reboot the phone)

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.

And this is some of my code :

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

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

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>

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.

I'm not experienced on Android, so anyone please give me some advice and correct my coding. Thanks a lot.

You manifest specifies that MainActivity is the one opened with the application as it has the options

<intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

If you want your process to work, you will have to make you DispatcherActivity the only main activity of your app by setting these options on it.

MainActivity is your launcher so it will always start.

Solution - In MainActivity onCreate read data from SharedPreference if it is Jfp1 open JFp1 screen if it is other activity start another activity and then call finish() on main activity.

Use this new code

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

UPDATE: Call finish() is much more appropriate, add this below

startActivity(new Intent(this, activityClass));
finish();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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