Search code examples
androidandroid-activitysharedpreferencesdispatcherandroid-reboot

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.


Solution

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