I was having some problem when trying to move to a new intent in onActivityResult. Here is the code:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(
requestCode, resultCode, intent);
if (scanningResult != null) {
barcode = scanningResult.getContents();
new GetBookByBarcode(
new GetBookByBarcode.OnRoutineFinished() {
public void onFinish() {
String bookID = GetBookByBarcode.bookID;
if (bookID.equals("")) {
Toast toast = Toast.makeText(getApplicationContext(),
"Book not found, please try another barcode.",
Toast.LENGTH_SHORT);
toast.show();
} else {
intent = new Intent(context, ReserveBook.class);
intent.putExtra("BookID", bookID);
startActivity(intent);
BarcodeScan.this.finish();
}
}
}).execute();
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
}
Firstly, I will perform a barcode scan. When onActivityResult, I will execute the AsyncTask class. When the AsyncTask class onFinish, I then move to a new intent. However, I am getting error message:
Cannot refer to non-final variable intent inside inner class defined in different method.
at the intent inside the else statement. Any ideas? Thanks in advance.
Just declare a new Intent variable. You're creating a new Intent anyways, so there's no point trying to reference the non-final parameter from onActivityResult.
The reason you get this message from the compiler is because only final variables (and effectively final variables in Java 8) can be referenced inside an anonymous inner class like that.
Instead of doing
intent = new Intent(context, ReserveBook.class);
intent.putExtra("BookID", bookID);
startActivity(intent);
BarcodeScan.this.finish();
Try doing
Intent newIntent = new Intent(context, ReserveBook.class);
newIntent.putExtra("BookID", bookID);
startActivity(newIntent);
BarcodeScan.this.finish();
Because as of now, you are trying to access the parameter from onActivityResult which gives you the error.
Related
I am trying to display a toast message after the user gets to the main intent.
I try to accomplish that by doing this by the code below but with no success:
On the second activity(MapsActivity) I did:
public void userLocationIsNull(){
Intent intent = new Intent(MapsActivity.this, MainActivity.class);
startActivityForResult(intent,1010);
}
And on the first activity(MainActivity) I did:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1010) {
Toast.makeText(this, "System could find your location,please try again", Toast.LENGTH_SHORT).show();
}
}
The intent is working,but the Toast is'nt showing,glad to hear your thoughts,thanks.
Insted of using
Toast.makeText(this, "System could find your location,please try again", Toast.LENGTH_SHORT).show();
try :
Toast.makeText(getApplicationContext(), "System could find your location,please try again", Toast.LENGTH_SHORT).show();
You should check into the use of onActivityResult() here.
Your onActivityResult() will be called in the activity in which you create the intent itself(in this case your MapsActivity) and not in any other Activity (here MainActivity).
Update
Ok so once you have used this method correctly, try this inside your if statement for requestCode
if (resultCode == Activity.RESULT_OK) {
// your toast here
}
else{
// the Activity result is null, and so your 'if' statement is not working
}
Tell me if this helps or not
I have 2 Activities. On 2nd activity i have data which i want to move by button "Back" to 1st Activity.
Usually i moving by something like this:
button12.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pass=editText2.getText().toString();
Intent intent = new Intent(FirstActivity.this, MapsActivity.class);
intent.putExtra("pass_value", pass);
startActivity(intent);
}
});
But now i dont want to start Activity, instead of this i want close Activity, so i need to use finish()
Currently i created this, but no working:
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, FirstActivity.class);
intent.putExtra("number", numberOfTrue);
finish();
}
});
I need something more in this code, but i dont know what.
Start a new activity with
Intent intent = new Intent(FirstActivity.this, MapsActivity.class);
intent.putExtra("pass_value", pass);
startActivityForResult(intent,1)
You can use setResult method to acheive your desired result
Intent output = new Intent();
output.putExtra("number", numberOfTrue);
setResult(Activity.RESULT_OK, output);
finish();
get Your result in FirstActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {
int num = data.getIntExtra("pass_value");
}
}
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
finish();
Intent intent = new Intent(this, FirstActivity.class);
intent.putExtra("number", numberOfTrue); }});
You need to open by function startActivityForResult
startActivityForResult(Intent intent, int requestCode)
And get the data returned in the function
onActivityResult
As an example you can see a face to the camera for a picture
I have an Activity A and a Fragment B.
In A I have this method:
private void installApp(String path){
Intent i = new Intent(Intent.ACTION_VIEW);
fileUri = Uri.fromFile(new File(path));
i.setDataAndType(fileUri, "application/vnd.android.package-archive");
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
B.startActivityForResult(i, 101); //B has been initialized elsewhere
}
Which later calls this override in B:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
try{
super.onActivityResult(requestCode, resultCode, data);
//Can't d stuff with data because it's Null.
}
}catch (Exception e){
//Handles the exception
}
}
I've been searching for a while now and still can't figure it out why data is null. What did I miss?!
Thank you.
In Fragment B are you calling startActivityForResult(new Intent(getActivity(), ActivityA.class), REQUEST_CODE) ? (REQUEST_CODE being an int)
if so then:
In Activity A you are not starting an Activity for a result. you need to call: finish();
in Activity A rather than
startActivityForResult();
update your code to look like:
private void installApp(String path){
Intent i = new Intent(Intent.ACTION_VIEW);
fileUri = Uri.fromFile(new File(path));
i.setDataAndType(fileUri, "application/vnd.android.package-archive");
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
setResult(RESULT_OK, i);
finish();
}
The documentation for ACTION_VIEW has "Output: nothing", which means that ACTION_VIEW implementations do not supply a result, and so using ACTION_VIEW with startActivityForResult() is pointless.
I am making a music player application, and I am trying to implement playlists. I have a file chooser in another intent, and I would like the ListView in the mainActivity to update when the file chooser intent closes. how can I call my UpdateListView method when it closes?
start intent:
Intent intent = new Intent(this, FileChooser.class);
startActivity(intent);
Closing intent
public void closeButton(View view){
finish();
}
Any help would be appreciated! thanks!
I assume you are using your own FileChoser class, not a standard Android one:
private static final int FileChooserRequestCode = 666;
Intent intent = new Intent(this, FileChooser.class);
startActivityForResult(intent, FileChooserRequestCode);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FillChooserRequestCode) {
if (resultCode == Activity.RESULT_OK) {
// ... file is chosen
String fileName = data.getStringExtra("FileName");
} else {
... dialog is closed
}
}
}
in FileChoser you do
Intent intent = new Intent();
intent.putStringExtra("FileName", fileName);
SetResult(Activity.RESULT_OK, intent);
finish();
and
SetResult(Activity.RESULT_CANCELED);
finish();
You can use startActivityForResult() please refer the link Getting Results From Activity
static final int FILE_CHOOSER_INTENT = 1; // The request code
...
private void chooseFile() {
Intent intent = new Intent(this, FileChooser.class);
startActivityForResult(intent, FILE_CHOOSER_INTENT);
}
Call setResult pass your result data as Intent. for details refer link SetResult function
Override this in your calling activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == FILE_CHOOSER_INTENT) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}
I have a problem with my code. I want to pass a String from the SecondActivity to FirstActvity. Note that the FirstActivity is not visible but its still open. when the SecondActivity is finish it passes a String to the FirstActivity.
My problem here is that when the SecondActivity ended and goes to FirstActivity, the whole application closes.
FirstActivity to SecondActivity:
Intent intent = new Intent(MainActivity.this, FileChooser.class);
startActivityForResult(intent, 0);
SecondActivity to FirstActivity:
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("filePath", "/sdcard/path1");
setResult(0);
finish();
FirstActivity Result:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//TODO handle here.
Intent intent = getIntent();
this.filePath = intent.getExtras().getString("filePath");
}
What is wrong with the code?
When you set the result of your SecondActivity, you only set the result code. Instead of setResult(0) use setResult(0,intent)
Also, in your FirstActivity's onActivityResult get the extra from the data argument - this.filePath = data.getExtras().getString("filePath");
Try to use
data.getExtras().getString("filePath");
instead of
intent.getExtras().getString("filePath");`
Try with Bundle:
First Activity;
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), FIRSTACTIVITY.class);
Bundle bundle = new Bundle();
bundle.putString("filePath","/sdcard/path1");
intent.putExtras(bundle);
startActivity(intent);
}
Second Activity:
public void activity_value() {
Intent i = getIntent();
Bundle extras=i.getExtras();
if(extras !=null) {
value = extras.getString("filePath");
}
}
try this
example.It solves your problem.