2011-10-14 95 views
0

我有這段代碼,它通過步驟從webservice獲得。當通過活動返回時,應用程序停止

我實現了我自己的歷史記錄,以便用戶可以返回執行的步驟。 問題是,同一個步驟可能會被多次訪問,這似乎會導致我的歷史記錄失敗。

它可以恢復正常,但有時需要多次按返回按鈕才能轉到上一步。

這裏的管理歷史記錄我的類代碼:在我的方法

public void startChildActivity(String id, Intent intent) 
{ 
id += System.currentTimeMillis(); 
if ("restart".equalsIgnoreCase(id)) 
{ 
    idList.clear(); 
} 

Window window = getLocalActivityManager().startActivity(id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)); 
if (window != null) 
{ 
    idList.add(id); 
    setContentView(window.getDecorView()); 
} 

}

FLAG_ACTIVITY_CLEAR_TOP標誌

private ArrayList<String> idList; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    if (idList == null) 
    { 
     idList = new ArrayList<String>(); 
    } 
} 

@Override 
public void finishFromChild(Activity child) 
{ 
    LocalActivityManager manager = getLocalActivityManager(); 
    int index = idList.size()-1; 

    if (index < 1) 
    { 
     finish(); 
     return; 
    } 

    manager.destroyActivity(idList.get(index), true); 
    idList.remove(index); index--; 
    String lastId = idList.get(index); 
    Activity lastActivity = manager.getActivity(lastId); 
    Intent lastIntent = lastActivity.getIntent(); 
    Window newWindow = manager.startActivity(lastId, lastIntent); 
    setContentView(newWindow.getDecorView()); 
} 

public void startChildActivity(String id, Intent intent) 
{ 
    id += System.currentTimeMillis(); 
    if ("restart".equalsIgnoreCase(id)) 
    { 
     idList.clear(); 
    } 

    Window window = getLocalActivityManager().startActivity(id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)); 
    if (window != null) 
    { 
     idList.add(id); 
     setContentView(window.getDecorView()); 
    } 
} 

public Activity getCurrentActivity() 
{ 
    int length = idList.size(); 
    if (idList.isEmpty()) { 
     return null; 
    } 
    else 
    { 
     return getLocalActivityManager().getActivity(idList.get(length-1)); 
    } 
} 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) 
{ 
    if (keyCode == KeyEvent.KEYCODE_BACK) 
    { 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 

@Override 
public boolean onKeyUp(int keyCode, KeyEvent event) 
{ 
    if (keyCode == KeyEvent.KEYCODE_BACK) 
    { 
     onBackPressed(); 
     return true; 
    } 
    return super.onKeyUp(keyCode, event); 
} 

@Override 
public void onBackPressed() 
{ 
    int length = idList.size(); 
    if (length > 1) 
    { 
     Activity current = getLocalActivityManager().getActivity(idList.get(length-1)); 
     current.finish(); 
    } 
} 

回答

0

匝當時搞亂的東西了,現在一切都很完美。

相關問題