Issue finishing Activity
I have a project running on where i call the ZXing scanner on button push, after scanning the bardcode another activity comes up where it shows the scanned data and i can put in an amount. Then i got 2 buttons finish, which will go back to the main window , and the next button which closes this activity and calls ZXing again, after scanning the code the window (should) come up again where i can put in the amount so on .. so on... but after the first next button push the amount input window wont come up and it puts me back to the main window. I got the 2 activity codes here.
This is my main activity where i call the ZXing the first time on button push. package org.example.sudoku;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.app.AlertDialog;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class Sudoku extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up click listeners for all the buttons
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
View scanButton = findViewById(R.id.scan_button);
scanButton.setOnClickListener(this);
View editButton = findViewById(R.id.about_button);
editButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.scan_button:
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "ONE_D_MODE");
startActivityForResult(intent, 0);
break;
case R.id.about_button:
Intent about = new Intent(this.getApplicationContext(),About.class);
Bundle b = new Bundle();
b.putString("key","blablabla");
about.putExtras(b);
startActivityForResult(about, 0);
break;
case R.id.exit_button:
finish();
break;
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
//String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
Intent result = new Intent(this.getApplicationContext(),Result.class);
Bundle b = new Bundle();
b.putString("content",contents);
result.putExtras(b);
startActivityForResult(result, 0);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
/*protected void onActivityResult(int requestCode, int resultCode,Intent data) {
switch(requestCode) {
case IntentIntegrator.REQUEST_CODE: {
if (resultCode != RESULT_CANCELED) {
IntentResult scanResult =IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanResult != null) {
String upc = scanResult.getContents();
Intent result = new Intent(this.getApplicationContext(),Result.class);
Bundle b = new Bundle();
b.putString("content",upc);
result.putExtras(b);
startActivityForResult(result, 0);
}
}
break;
}
}
}*/
}
And this is my upcoming activity after the scan is done.
package org.example.sudoku;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.content.Intent;
public class Result extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
Bundle b = getIntent().getExtras();
String product = b.getString("content").toString();
TextView et1 = (TextView) findViewById(R.id.edit_text);
et1.setText(product);
View finishButton =findViewById(R.id.finish_button);
finishButton.setOnClickListener(this);
View nextButton =findViewById(R.id.next_button);
nextButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.finish_button:
finish();
break;
case R.id.next_button:
Intent intent2 = new Intent("com.google.zxing.client.android.SCAN");
intent2.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "ONE_D_MODE");
startActivityForResult(intent2, 0);
finish();
break;
}
}
public void onActivityResult(int requestCode, int resultCode, Inten开发者_如何学Got intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
Intent result = new Intent(this.getApplicationContext(),Result.class);
Bundle b = new Bundle();
b.putString("content",contents);
result.putExtras(b);
startActivityForResult(result, 0);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
}
On this activity are the 2 buttons , finish and next, the finish button works fine it closes the activity well, but the next button is the problem it calls the ZXing , but after the scan it puts me back to the main activity. But if i dont call the finish() on this button the after im done with like 10 scans i have to close all the activitys.
I could need a bit help here. Thanks in front.
In your Result class, shouldn't you remove finish()?
case R.id.next_button:
Intent intent2 = new Intent("com.google.zxing.client.android.SCAN");
intent2.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "ONE_D_MODE");
startActivityForResult(intent2, 0);
finish(); // REMOVE THIS
break;
As you are starting the activity, expecting a result, but the activity has finished?
If you dont finish it the class will receive the result and start another result class and THEN you should finish
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
Intent result = new Intent(this.getApplicationContext(),Result.class);
Bundle b = new Bundle();
b.putString("content",contents);
result.putExtras(b);
startActivityForResult(result, 0);
finish(); // NEW FINISH
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
精彩评论