如何取得父标签活动的子活动的结果呢?标签、结果

2023-09-12 06:07:18 作者:逗逼i

我有活性2,它是具有子活动Activity3和Activity4.Acticity2一个TabActivity被称为从Activity1.I想对这款从子活动的结果(Activity3或Activity4)在Activity2.Any帮助...?

I have Activity2 which is a TabActivity having child activities Activity3 and Activity4.Acticity2 is called from Activity1.I want results from child activity(Activity3 or Activity4) in Activity2.Any help on this...?

推荐答案

使用 startActivityForResult 而不是 startActivity 启动Activity3和Activity4。 使用的setResult 在你的孩子的活动将数据返回到predecessor活动 使用 onActivityResult 在父活动的子活动收到的结果

Use startActivityForResult instead of startActivity to start Activity3 and Activity4. Use setResult in your child activity to return data to the predecessor activity Use onActivityResult in your parent activity to receive the result from the child activity

编辑:添加了捆绑信息。保持原来的答案,因为它很可能会为别人有用。

Added bundle information. Keeping original answer as it will likely be useful for others.

既然你实际上并没有开始与 startActivity 的活动,您将需要从孩子的活动存储数据,试试这个:

Since you aren't actually starting the activity with startActivity, you will need to store your data from the child activities, try this:

在TabActivity:

In TabActivity:

// putExtra is overloaded so you can add almost any kind of data.
// First parameter is the key, second is the value
getIntent().putExtra ( "Result", "OK" );

在父活动:

// tabAct is the TabActivity object for your tab
// Here, just specify the key that you used in putExtra in your TabActivity
String actResult = tabAct.getStringExtra ( "Result" );
if ( actResult.equals ( "OK" ) {
    // Do your actions for success
}
else {
    // Do your actions for failure
}