startActivity()总是调用startActivityForResult()吗?

时间:2012-01-26 16:41:48

标签: android

startActivity()是否始终调用startActivityForResult()?例如:

public class ActivityTest extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btn = (Button)findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(
                    ActivityTest.this, ActivityFoo.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void startActivity(Intent intent) {
        Log.e("hi", "startActivity()");
        super.startActivity(intent);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        Log.e("hi", "startActivityForResult()");
        super.startActivityForResult(intent, requestCode);
    }
}

输出:

startActivity()
startActivityForResult()

由于

1 个答案:

答案 0 :(得分:6)

是的,因为startActivityForResult()的源代码如下所示:

public void startActivity(Intent intent) {
        startActivityForResult(intent, -1);
    }

他们这样写它是为了避免重复的代码。启动Activity的实际实现在startActivityForResult()中定义,如果传入-1的值,则只会忽略结果。