如何在活动之间传递事件数据?

时间:2013-04-11 09:12:38

标签: android android-intent android-activity

我想将活动数据从活动B传递给活动A.

活动A如下所示:

public class ActivityDemo extends Activity {

private Intent mIntent;

private Context mContext;

private TextView mText;

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

    mContext = getApplicationContext();

    Button button = (Button)findViewById(R.id.qbutton);

    if (button != null) {
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mIntent = new Intent(mContext, DualActivity.class);
                if (mIntent != null) {
                    startActivity(mIntent);
                }
            }
        });
    }
    mText = (TextView)findViewById(R.id.qText);
}
...
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        mText.setText(data.getStringExtra("TextOut"));
    }
}

活动B是这样的:

public class DualActivity extends Activity implements QFragment.OnDataPassToActivity {

private Context mContext;

private Intent mIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mContext = getApplicationContext();

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(SUIRes.layout(getApplicationContext(), "dual_default_activity"));

    mIntent = getIntent();
}

public void onDataPassToActivity(String string) {
    if(mIntent != null) {
        mIntent.putExtra("TextOut", string);
    }
    //Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show();
}
}

活动B最初由活动A调用,而onDataPassToActivity()是一个由另一个类调用的回调函数。我想在调用onDataPassToActivity()函数时将字符串数据发送到Activity A.我检查了评论Toast在调用回调时工作正常,但无法向Activity A发送意图。

这个问题还有其他解决办法吗?

0 个答案:

没有答案