跳过onCreate中的startActivityForResult

时间:2011-02-13 06:33:33

标签: android

所以我真的很茫然。我有我的主要onCreate尝试加载文件,如果文件不存在我想调用startActivityForResult来获取一些输入。不过,我的代码完全跳过了startActivityForResult。我从LogCat得到的东西,代码只是继续向前。在处理程序或其他东西中调用startActivityForResult可以正常工作,但它对我的应用程序无效。这是我的代码。

我的主要创作:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    findViews();  
    setListeners();
    Stuffs stuffs = DomUtil.getFullDom(this);
    if (stuffs != null) {
        // do stuff
    }
    else {
        startActivityForResult(new Intent(this, SelectType.class), 0);
        addNew(1, null);
    }
    currentScreen = 1;
}

我试图调用的SelectType的onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.selecttype);
    findView();
    setListeners();
}

我在这里做错了什么?我唯一能想到的就是在onCreate中调用startActivityForResult,但是我已经看过教程已经完成了。这是怎么回事?

2 个答案:

答案 0 :(得分:6)

startActivityForResult()不应返回结果;它开始指示的活动并继续。当另一个活动结束时,将使用结果调用onActivityResult()方法。这些活动是异步执行的。

答案 1 :(得分:0)

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TestActivityforResultsActivity extends Activity {
/** Called when the activity is first created. */
private final int PICK = 1;
int no1 = 30,no2 = 40;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ((TextView)findViewById(R.id.textView1)).setText("Number1 = "+no1);
    ((TextView)findViewById(R.id.textView2)).setText("Number2 = "+no2);
}

public void startActivity(View view) {
    Intent intent = new Intent();
    intent.setClass(view.getContext(), ResultScreen.class);
    intent.putExtra("no1", no1);
    intent.putExtra("no2", no2);
    startActivityForResult(intent, PICK);       
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);      
    int str = data.getIntExtra("YourResults", 10);
    ((TextView)findViewById(R.id.textView3)).setText("Result = "+str);
    ((Button)findViewById(R.id.button1)).setText("Result = "+str);
}  
}
  

resulClass

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class ResultScreen extends Activity{
int resul =0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result_screen);     
    System.out.println("Read This Class...."); 
    int no1 = getIntent().getIntExtra("no1", 0);
    int no2 = getIntent().getIntExtra("no2", 0);
    resul = no1+no2;
}

public void setResults(View view) {


    Intent intent = new Intent();
    intent.setClass(view.getContext(), TestActivityforResultsActivity.class);
    intent.putExtra("YourResults", resul);
    setResult(RESULT_OK,intent);
    finish();
}
}