Android等待另一个活动结果 - 它没有用

时间:2011-08-09 06:20:24

标签: android

如何等待android中的另一个活动结果。 我编写了以下代码,但在活动完成后,它总是转到onCreate()方法。

我有标签活动。从一个标签活动中,我调用另一个活动,即弹出窗口......完成弹出窗口后应该返回上一个活动

   @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Object o = this.getListAdapter().getItem(position);
    keyword = o.toString();
    positions = position;

    if(position != 0 ){
        Bundle bundle = new Bundle();

        Intent showContent = new Intent(getApplicationContext(),SalesRouteDevitionActivity.class);
        int postion = position;
        String aString = Integer.toString(postion);
        bundle.putString("positon", aString);
        showContent.putExtras(bundle);
        startActivityForResult(showContent,0);
    }else{

        Intent intent = new Intent(SalesRouteActivity.this, ListRetailerActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("RouteName", keyword);
        intent.putExtras(bundle);
        View view = SalesActivityGroup.group.getLocalActivityManager().startActivity("", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();  
        SalesActivityGroup.group.replaceView(view);
    }
}


   @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {  

       Log.w("onActivityResult" ," --requestCode ---" + requestCode + " -- resultCode -- " + resultCode + " -- data --" + data);
      if(resultCode==0){
          Toast.makeText(this, "Reason has been successfully.", Toast.LENGTH_LONG).show();
          if(resultCode==RESULT_OK)
              Toast.makeText(this, "Reason has been successfully.", Toast.LENGTH_LONG).show();
         else
              Toast.makeText(this, "Fail", Toast.LENGTH_LONG).show();
      }
  }

和SalesRouteDevitionActivity.class

    public class SalesRouteDevitionActivity extends Activity {
private String array_spinner[];
String param1 = "";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.route_diviation_popup);
    array_spinner=new String[2];
    array_spinner[0]="Rain";
    array_spinner[1]="Floods";

    Spinner s = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, array_spinner);
    s.setAdapter(adapter);
    Button button = (Button) findViewById(R.id.submit);

     button.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {


             Intent mIntent = new Intent();
             setResult(RESULT_OK);
             finish();



         }
     });

}

}

这是我的Tab Activity.I正在使用ActivityGroup

  public class MainActivity extends TabActivity {
int selectedTab;
TabHost tabHost ;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabview);

    TabHost t = getTabHost();
    tabHost = (TabHost)findViewById(android.R.id.tabhost);

    TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
    TabSpec secondTabSpec = tabHost.newTabSpec("tid1");
    TabSpec thirdTabSpec = tabHost.newTabSpec("tid1");
    /** TabSpec setIndicator() is used to set name for the tab. */
    /** TabSpec setContent() is used to set content for a particular tab. */
    firstTabSpec.setIndicator("Sales").setContent(new Intent(this,SalesActivityGroup.class));
    secondTabSpec.setIndicator("Admin").setContent(new Intent(this,SettingActivityGroup.class));
    thirdTabSpec.setIndicator("Setting").setContent(new Intent(this,SettingActivityGroup.class));


    tabHost.addTab(firstTabSpec);
    tabHost.addTab(secondTabSpec);
    tabHost.addTab(thirdTabSpec);
    tabHost.setCurrentTab(0);
    tabHost.setMinimumHeight(25);
}

public void onTabChanged(String arg0) {
        selectedTab = tabHost.getCurrentTab();

}

这是SalesActivityGroup.java

 public class SalesActivityGroup extends ActivityGroup {

public static SalesActivityGroup group;
private ArrayList<View> history;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.history = new ArrayList<View>();
    group = this;

    View view = getLocalActivityManager().startActivity("Sales",
            new Intent(this, SalesRouteActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
            .getDecorView();

    replaceView(view);

}

public void replaceView(View v) {
    history.add(v);
    setContentView(v);

}

public void back() {
    if (history.size() > 0) {
        history.remove(history.size() - 1);
        if (history.size() > 0) {
            setContentView(history.get(history.size() - 1));
        } else {
            finish();
        }
    } else {
        finish();
    }
}

@Override
public void onBackPressed() {
    SalesActivityGroup.group.back();
    return;
}

}

我知道这是我们应该做的事,但我真的不知道为什么它不能发挥作用......

我没有去onActivityResult()方法&amp;它没有打印Log

请帮帮我..

提前致谢

5 个答案:

答案 0 :(得分:4)

从SalesRouteDevitionActivity.class中删除startActivity(showContent); 编辑:

Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
finish();

答案 1 :(得分:3)

Android中没有控制台。所以System.out.println将不起作用。出于调试目的,您可以使用LogCatHere's how to use LogCat

答案 2 :(得分:3)

您无需致电

super.onActivityResult(requestCode, resultCode, data);

当你处理ONActivityResult时。用Log.i替换你的System.out.println

答案 3 :(得分:3)

替换此

if(resultCode==0)

用这个

if(requestCode==0)

答案 4 :(得分:1)

如果您使用的是Tab组,那么应该像这样使用:

致电活动:

 Intent showContent = new Intent(botton.getContext(),RetailerOrderEActivity.class);
 getParent().startActivityForResult(showContent, 2);

考虑getParent(),如果是Tab活动组,那么就这样调用。

和儿童活动:

    Intent i = new Intent();
    Bundle bundle = new Bundle();
    i.putExtras(bundle);
    setResult(Activity.RESULT_OK, i);
    finish();

完成后,这将转到ActivityGroup类:

你想这样打电话:

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(requestCode ==1){
        Bundle bundle = data.getExtras();
        String roteCode = bundle.getString("RouteCode");
        Intent intent = new Intent(SalesActivityGroup.this,ListRetailerActivity.class);
        bundle.putString("RouteCode", roteCode);
        intent.putExtras(bundle);
        View view = SalesActivityGroup.group.getLocalActivityManager().startActivity("",intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
        SalesActivityGroup.group.replaceView(view);
    }
   }