Search code examples
android

In ActivityGroup, how to restore the previous activity (without restarting)


I have an ActivityGroup which has 4 activities in a Tab.

A1->A2->A3->A4

Suppose I press back from any activity the previous activity is being restarted. My requirement when I call a new Activity it should be started fresh. and when I press the back button, the previous activity has to be restored (without restarting).

I'm lacking in setting appropriate Intent Flags. My code goes like this.

      public void startChildActivity(String Id, Intent intent) {
        
        Window window;
        Log.e("startChildActivity","startChildActivity");
        
            window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        
            if (window != null) 
            {
                mIdList.add(Id);
                
                setContentView(window.getDecorView());
            }
            
            
    } 


        public void finishFromChild(Activity child) 
    {
        restartFlag=true;
        
        Log.e("finishFromChild","finishFromChild");
        LocalActivityManager manager = getLocalActivityManager();
        
        int index = mIdList.size()-1;
        
        if (index < 1) {
            
            finish();
            
            return;
        }
        
        manager.destroyActivity(mIdList.get(index), true);
        
        mIdList.remove(index);
        
        index--;
        
        String lastId = mIdList.get(index);
        
        Intent lastIntent = manager.getActivity(lastId).getIntent();
        
        Window newWindow = manager.startActivity(lastId, lastIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        
        setContentView(newWindow.getDecorView());
    
    }

Solution

  • I finally got a solution for this; the culprit was

       manager.destroyActivity(mIdList.get(index), true); 
    

    in the code.

    There is a bug in Android destroyActivity() function implementation.

    The exact source of the problem is in the following chunk of code in LocalActivityManager.java:

    public Window destroyActivity(String id, boolean finish) {
        LocalActivityRecord r = mActivities.get(id);
        Window win = null;
        if (r != null) {
            win = performDestroy(r, finish);
            if (finish) {
                mActivities.remove(r);
            }
        }
        return win;
    }
    

    The variable mActivities is the hashmap containing the activity records and it uses the id passed into startActivity() as the key. In this method, the object passed in for the key is a LocalActivityRecord instead of the id string. This results in the hashmap not finding the entry and thus not removing it.

    More info refer this link. http://code.google.com/p/android/issues/detail?id=879More