Question

I am trying to control my stop/restart behavior according to the Android Guidelines but it's getting a bit tricky. I want my app to restore state after being minimized and tapping the launcher icon.

The current behaviour makes so I can HOME out of the app, and when the user comes back using the long press list everything behaves as expected. Now, trying to return tapping the app icon starts the default activity, it doesn't take onRestoreInstanceState into account, and while keeping the old task affinities open on their back stack.

The activities have onCreate, onResume, onSavedInstanceState overridden, calling their super, and are of type SingleInstance.

Was it helpful?

Solution

During one of my app development, I had the similar issue where app have single activity and need to save some state values (usually string, Boolean) onpause. I will give you idea how i handle it, and I hope it will useful to you also.

 protected void onPause() {
         super.onPause(); 
            saveState();

   }

  protected void onResume() {  
    super.onResume();       
    retrieveState();
  }

donot forget focus change ...

   @Override
    public void onWindowFocusChanged(boolean hasFocus) {  
      super.onWindowFocusChanged(hasFocus);
      if(hasFocus) {            
        retrieveState(); 
      }
      else { saveState(); } 
  }

Now the save and retrieve data

 public void saveState()
 {                  

     SharedPreferences settings = getSharedPreferences(WEBSTATE, 0);
 SharedPreferences.Editor editor = settings.edit();

    editor.putString(CURRENT_URL_STATE, "some url");
    editor.putInt(FILECOUNT, 10);
    /* any other content... */
   editor.commit();  

  }  


 public void retrieveState()
 {

    SharedPreferences settings = getSharedPreferences(WEBSTATE, 0);
    String current_url = settings.getString(CURRENT_URL_STATE, "");

    int count = settings.getInt((FILECOUNT, 0);

    /*use these value as per requier.. */
  }

OTHER TIPS

The current behaviour makes so I can HOME out of the app, and when the user comes back using the long press list everything behaves as expected. Now, trying to return tapping the app icon starts the default activity, it doesn't take onRestoreInstanceState into account, and while keeping the old task affinities open on their back stack.

Let's say you got 2 activities: A and B. Activity A is your entry point, so when you tap on app icon (which in fact is incorrect statement as this is activity icon, NOT the app icon) you launch activity A. Now you got to activity B and press HOME. When you now open list of recents, you will see activity B, and will be able to get back to it. But when you tap your "app icon" on launcher screen, you will enter activity A. And this is normal and correct behavior. Also note savedInstanceState is not overall state of your application as you most likely think.

Also note, that your Activity class can be instantiated multiple times, which depending on your application architecture can, also lead to incorrect navigation behaviour. You can try to control this by using android:launchMode in your Manifest file (you will find android documentation here).

Finally, if possible, consider reworking your application to use Fragments instead of multiple activities - that would simplify controling application flow.

Not sure if this is what you want to do, But i had a similar problem where i wanted a single base activity to return to, And i couldn't get the desired result from SingleInstance, I defined a new Activity which was the main launched :

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

And from that activity i launched my desired Activity with the desired FLAG clear-top, and finished the main launched activity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top