0

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.

  • what is your exact use case? this might lead to more problems in the future especially if your Dispatcher jumps to a screen, where the screen may have been part of a flow and assumes the back stack contains the previous screens in the flow... and if the activity your jumping to is truly of depth=1 starting from your dispatcher, then why perform a jump at all? the UX should provide a straight-forward path to the activity.... these are just suggestions, knowing your exact use case would help. – Ryhan Feb 25 '16 at 08:28

3 Answers3

1

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.

malrok44
  • 592
  • 6
  • 17
  • I have tried as you suggested, and It is work okay. . But another this happened, that is : after reboot HP and Jfp1 screen appeared, and I pressed 'back' button on HP, it is showed only blank screen. How to avoid that, so I can directly closed the apps without blank screen appeared ? Thanks a lot for your advice. – Leonard Mamangkey Feb 26 '16 at 04:38
1

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.

sanky jain
  • 873
  • 1
  • 6
  • 14
1

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();
Huteri
  • 158
  • 2
  • 12
  • I have tried as you suggested, and It is work okay. . But another this happened, that is : after reboot HP and Jfp1 screen appeared, and I pressed 'back' button on HP, it is showed only blank screen. How to avoid that, so I can directly closed the apps without blank screen appeared ? Thanks a lot for your advice. – Leonard Mamangkey Feb 26 '16 at 04:39
  • @LeonardMamangkey it is because DispatcherActivity is a blank page. Add intent flags Intent.FLAG_ACTIVITY_NEW_TASK – Huteri Feb 26 '16 at 05:49
  • where should I put that ? on the manifest ? or on the DispatcherActivity.java ? Could you please give an example for that ? Appreciate that.very much. – Leonard Mamangkey Feb 26 '16 at 08:52
  • Seems like you already got the answer, use finish() is simpler. – Huteri Feb 26 '16 at 09:16
  • thanks a lot, it is works okay and you saved my days. – Leonard Mamangkey Feb 29 '16 at 03:26