android - 如何在选项卡中使用startActivityForResult()

时间:2012-04-13 12:30:19

标签: android android-tabhost

我的应用有3个标签。在这一个选项卡中有多个活动(意味着此选项卡具有导航到子选项卡)。我在一个子活动中使用了startActivityforResult()。但控制永远不会转到onActivityResult()方法。如何实现这一点。请任何人帮助我。

public class Activity_1 extends Activity
{


public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    View contentView = LayoutInflater.from(getParent()).inflate(R.layout.static_search_filters, null);  
    setContentView(contentView);  

    states_tv = (TextView)findViewById(R.id.state);
    states_tv.setOnClickListener(states_etListener);        
}

private OnClickListener states_etListener = new View.OnClickListener()
{
    public void onClick(View v)
    {               
        Intent intent = new Intent(getParent(), RB_CategoriesMList.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivityForResult(intent,GET_SEL_STATES_LIST);             
    }       
};

 protected void onActivityResult(int requestCode, int resultCode, Intent data) 
 {
      super.onActivityResult(requestCode, resultCode, data);    
      //I do some stuff here
 }
}   

// Activity_2

public class RB_CategoriesMList extends ListActivity
{



public Button sbtn;   

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.categories_list);           

    sbtn =(Button)findViewById(R.id.submit_categories);
    sbtn.setOnClickListener(sbtn_listener);
}       

private OnClickListener sbtn_listener = new View.OnClickListener()
{
    public void onClick(View v)
    {
        Intent state_intent = getIntent();              
        state_intent.putExtra("selected_states", "");                                       
        setResult(RESULT_OK,state_intent);
        finish();           
    }       
};  
}   

2 个答案:

答案 0 :(得分:0)

不要打电话

  super.onActivityResult(requestCode, resultCode, data);    

当您收到onActivityResult时,请检查请求代码是否是您的,然后执行您的操作。在其他情况下,你应该打电话给超级。

所以:

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if (requestCode == GET_SEL_STATES_LIST) {
        // Do your stuff
    } else {
        super.onActivityResult(requestCode, resultCode, data);    
    }
}

答案 1 :(得分:0)

getParent().startActivityForResult(yourNewIntent, yourResult);