我将Zxing库集成到我的项目中,我有:
1. MainActivity
带按钮,启动CaptureActivity
2.带有textView的SecondActivity。
我需要将CaptureActivity
的结果代码放到SecondActivity
的textView中,我将一些代码放到onActivityResult
的{{1}}中:
CaptureActivity
但什么都没发生,哪里错了?我在stackoverflow上阅读了很多内容,但无法理解
我的主要活动:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode == RESULT_OK) {
if (requestCode == HISTORY_REQUEST_CODE) {
Intent intent1=new Intent(this, SecondActivity.class);
startActivity(intent1);
int itemNumber = intent.getIntExtra(Intents.History.ITEM_NUMBER, -1);
if (itemNumber >= 0) {
HistoryItem historyItem = historyManager.buildHistoryItem(itemNumber);
decodeOrStoreSavedBitmap(null, historyItem.getResult());
}
}
}
}
从方法onCapture i开始Zxing的CaptureActivity,
SecondActivity:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onCapture(View view) {
Intent data=new Intent(this, CaptureActivity.class);
startActivity(data);
}
答案 0 :(得分:1)
您启动此活动:
public void onCapture(View view) {
Intent data=new Intent(this, CaptureActivity.class);
startActivity(data);
}
但你需要使用startActivityForResult
您还需要将onActivityResult代码移动到MainActivity中,因此流程将
MainActivity - > CaptureActivity - > MainActivity - >启动第二项活动
答案 1 :(得分:0)
您必须使用puteextra将数据从一个活动发送到另一个活动。
Intent i = new Intent(this, SecondActivity.class);
String s = "whatever";
i.putExtra("YOU_NAME_IT", s);
startActivity(i);
然后,在SecondActivity中你必须得到那个字符串。
Extras extras = getIntent().getExtras();
extras.getString("YOU_NAME_IT");
希望它有所帮助!