通过活动返回时,应用程序停止

时间:2011-10-14 09:13:14

标签: android android-activity

我有这个代码,通过网络服务获取的步骤。

我实现了自己的历史记录,以便用户可以返回执行的步骤。 问题是同一步骤可以被访问多次,这似乎导致我的历史失败。

它可以恢复正常,但有时按下后退按钮需要多次按下才能进入上一步。

以下是管理历史的班级代码:

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

1 个答案:

答案 0 :(得分:0)

转为FLAG_ACTIVITY_CLEAR_TOP标志

在我的方法中

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

}

搞砸了,现在一切都运转良好。