如何使用`startActivityForResult`开始不同的活动?

时间:2016-05-28 03:19:14

标签: android android-intent

如何使用startActivityForResultActivity启动多个Activity

Public class MainActivity extends Activity{

    TextView hasilscan1;
    Button btnInputPerangkat, btnEditPerangkat;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        hasilscan1 = (TextView) findViewById(R.id.hasilscan1);
        btnInputPerangkat = (Button) findViewById(R.id.btnInputPerangkat);
        btnEditPerangkat = (Button) findViewById(R.id.btnEditPerangkat);

        btnInputPerangkat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hasilscan1.setVisibility(TextView.INVISIBLE);
                IntentIntegrator scanIntegrator = new IntentIntegrator(MainActivity.this);
                scanIntegrator.initiateScan();
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
            if (scanningResult != null) {
                String scanContent = scanningResult.getContents();
                hasilscan1.setText(scanContent);
                Intent intent = new Intent(this, InputPerangkat.class);
                intent.putExtra("hasilscan1", hasilscan1.getText().toString());
                startActivity(intent);
            } else {
                Toast.makeText(getApplicationContext(), "Not found", Toast.LENGTH_SHORT).show();
                btnInputPerangkat.setVisibility(EditText.INVISIBLE);
            }
    }
}

在我的情况下,我在1个布局中有2个按钮,当点击按钮时,它将扫描条形码并且结果扫描将以另一个布局显示。我尝试了1个按钮并取得了成功,但我不知道如何实现2个按钮。

4 个答案:

答案 0 :(得分:0)

您需要为每个startActivityForResult(intent, REQUEST_CODE);提供不同的请求代码,并在onActivityResult

中查看
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);   
    if(requestCode == IntentIntegrator.REQUEST_CODE && resultCode = RESULT_OK) {
        IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (scanningResult != null) {
            String scanContent = scanningResult.getContents();
            hasilscan1.setText(scanContent);
            Intent intent = new Intent(this, InputPerangkat.class);
            intent.putExtra("hasilscan1", hasilscan1.getText().toString());
            startActivity(intent);
        } else {
            Toast.makeText(getApplicationContext(), "Not found", Toast.LENGTH_SHORT).show();
            btnInputPerangkat.setVisibility(EditText.INVISIBLE);
        }
    }
}

答案 1 :(得分:0)

您可以轻松完成。 startActivityForResult的使用是针对特定需求的,例如,您有Activity FirstActivity。它需要获取仅在SecondActivityThirdActivity上可用的值才能检索此值。

然后你必须定义两个请求代码(每个活动要启动一个)。 例如:

// Declaration 
private int firstRequestCode = 101;
private int secondRequestCode = 102;

// Create Intents
Intent firstIntent = new Intent(FirstActivity.this,SecondActivity.class);
Intent secondIntent = new Intent(FirstActivity.this,SecondActivity.class);

// On Fisrt Button Click
startActivityForResult(firstIntent,firstRequestCode);

// On Second Button Click
startActivityForResult(secondIntent,secondRequestCode);


// On Activity Result
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);   

/*if(requestCode == firstRequestCode  && resultCode = RESULT_OK) {

}

if(requestCode == secondRequestCode  && resultCode = RESULT_OK){

}*/


if(resultCode = RESULT_OK)

      if(requestCode == firstRequestCode)
      {
         // Open First Activity  
      }

      if(requestCode == secondRequestCode)
      {
         // Open Second Activity  
      }
}

答案 2 :(得分:0)

例如,点击按钮

Intent intentGetMessage = new Intent(this, OtherActivity.class);
            startActivityForResult(intentGetMessage, 25);

其他活动

            Intent intent = new Intent();
            intent.putExtra("check_list", 1);
            intent.putExtra("shared_contact", stList);
            setResult(25, intent);
            finish();

此代码位于私密活动

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

            if (requestCode == 25) {
                if (data != null) {
}
}

从数据中获取结果

答案 3 :(得分:0)

(代表OP发表。)

以下是解决方案:

startActivityForResult(scanIntegrator.createScanIntent(), REQUEST_CODE); 

public class MainActivity extends Activity{

TextView hasilscan1, hasilscan2;
Button btnInputPerangkat, btnEditPerangkat;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    hasilscan1 = (TextView) findViewById(R.id.hasilscan1);
    btnInputPerangkat = (Button) findViewById(R.id.btnInputPerangkat);
    btnEditPerangkat = (Button) findViewById(R.id.btnEditPerangkat);

    btnInputPerangkat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            hasilscan1.setVisibility(TextView.INVISIBLE);
            IntentIntegrator scanIntegrator = new IntentIntegrator(MainActivity.this);
            startActivityForResult(scanIntegrator.createScanIntent(), 10);
        }
    });

    btnEditPerangkat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            hasilscan1.setVisibility(TextView.INVISIBLE);
            IntentIntegrator scanIntegrator = new IntentIntegrator(MainActivity.this);
            startActivityForResult(scanIntegrator.createScanIntent(), 20);
        }
    });
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode==10) {
        if (resultCode == RESULT_OK) {
            String scanContent = intent.getStringExtra("SCAN_RESULT");
            hasilscan1.setText(scanContent);
            Intent intent1 = new Intent(this, InputPerangkat.class);
            intent1.putExtra("hasilscan1", hasilscan1.getText().toString());
            startActivity(intent1);
            } else if(resultCode==RESULT_CANCELED){
            Toast.makeText(getApplicationContext(), "Not found", Toast.LENGTH_SHORT).show();
         }
    }
    if (requestCode==20) {
        if (resultCode == RESULT_OK) {
            String scanContent = intent.getStringExtra("SCAN_RESULT");
            hasilscan1.setText(scanContent);
            Intent intent2 = new Intent(this, EditPerangkat.class);
            intent2.putExtra("hasilscan1", hasilscan1.getText().toString());
            startActivity(intent2);
        } else if(resultCode==RESULT_CANCELED){
            Toast.makeText(getApplicationContext(), "Not found", Toast.LENGTH_SHORT).show();
        }
    }
}