I am trying to open this class on the click of a button, it works fine, however, this class is not present in HTC devices, so i want my app to show a toast on this exception, but it doesn't show anything-
Intent intent = new Intent();
intent.setAction("android.media.action.DISPLAY_AUDIO_EFFECT_CONTROL_PANEL");
if ((intent.resolveActivity(getPackageManager()) != null)) {
startActivity(intent);
} else {
Toast.makeText(getBaseContext(), "you are offline", Toast.LENGTH_LONG).show();
// No equalizer found :(
}
You can open with
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
You can return by pressing back button on device.
Try this
In your MainActivity.java put
Intent intent = new Intent();
intent.setAction("android.media.action.DISPLAY_AUDIO_EFFECT_CONTROL_PANEL");
if ((intent.resolveActivity(getPackageManager()) != null)) {
// here is the changes
// REQUEST_CODE is an any integer value
startActivityForResult(intent, REQUEST_CODE);
} else {
Toast.makeText(getBaseContext(), "Unable to open setting..", Toast.LENGTH_LONG).show();
}
If you want to handle result of an intent then
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// add your code here
}
Related
I'm trying to use onActivityResult to send a title, I've looked at the google implementation for the same task but it doesn't work me. Can someone help?
code for onActivityResult in mainActivity.
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
System.out.println("There is something coming to this function" + requestCode);
if(requestCode == NEW_TITLE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK){
Title title = new Title(data.getStringExtra(notesSection.EXTRA_REPLY));
mTitleViewModel.insert(title);
}else{
Toast.makeText(getApplicationContext(), R.string.empty_not_saved, Toast.LENGTH_LONG).show();
}
}
code in my notesSection activity.
finishFab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// System.out.println("Button Has Been Clicked From Notes Section");
Intent replyIntent = new Intent();
if (TextUtils.isEmpty(titleEdit.getText())){
// System.out.println("Empty?");
setResult(RESULT_CANCELED, replyIntent);
}else{
// System.out.println("Sending Something Supposedly");
String title = titleEdit.getText().toString();
// System.out.println("Sending " + title);
replyIntent.putExtra(EXTRA_REPLY, title);
setResult(RESULT_OK, replyIntent);
}
finish();
// startActivity(new Intent(notesSection.this, MainActivity.class));
}
});
FYI: When I print something in the onActivityResult function, nothing shows up on my run terminal, I don't know why this is, but I don't think the function is being reached for some reason. I will send more code if necessary.
Thanks.
In your first activity try starting second activity like this
static int NEW_TITLE_ACTIVITY_REQUEST_CODE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
---
---
---
// i assume you are starting activity on some button click
// so add following line in you button on click event
startActivityForResult(new Intent(MainActivity.this,notesSection.class),NEW_TITLE_ACTIVITY_REQUEST_CODE);
}
then override following method in your FIRST ACTIVITY
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == NEW_TITLE_ACTIVITY_REQUEST_CODE)
{
// do whatever you want
}
}
Also update your finish fab on click listener
finishFab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent replyIntent = new Intent();
if (TextUtils.isEmpty(titleEdit.getText())){
setResult(RESULT_CANCELED, replyIntent);
}
else{
replyIntent.putExtra("TITLE", titleEdit.getText().toString());
setResult(RESULT_OK, replyIntent);
}
finish();
}
});
Actually, this procedure has changed recently. You might want to take a look at the official documentation so that you can implement it according to your needs.
I have the following code
public void changeContentOnClick(View view) {
Intent intent = new Intent(this, ChangeNodeContentActivity.class);
intent.putExtra("SELECTED_NODE_ID", selectedNode.getNodeId());
intent.putExtra("SELECTED_NODE_CONTENT", selectedNode.getNodeContent());
startActivityForResult(intent,RESULT_OK);
Log.d(TAG, "Can I get here?");
onActivityResult(RESULT_OK, RESULT_OK, intent);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
String editedNodeId = data.getStringExtra("EDITED_NODE_ID");
String editedNodeContent = data.getStringExtra("EDITED_NODE_CONTENT");
Node nodeChecker = new Node(editedNodeId);
Node editedNode = new Node(editedNodeId, editedNodeContent);
Log.d(TAG, editedNode.toString());
nodes.set(nodes.indexOf(nodeChecker), editedNode);
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
Somehow, after startActivityForResult method is called, everything stops. The log with message "Can I get here?" is never printed and I don't understand why not. I have followed the answer to this topic, but somehow couldn't make it work. Do I call the onActivityResult in the wrong way or place? Please help me out!
The that should send back some info from the ChangeNodeContentActivity and the one that handles the result code is the following on click listener:
public void changeContentOnClick(View view) {
Intent intent = getIntent();
selectedNode.setNodeContent(nodeContentDisplay.getText().toString());
intent.putExtra("EDITED_NODE_ID", selectedNode.getNodeId());
intent.putExtra("EDITED_NODE_CONTENT", selectedNode.getNodeContent());
setResult(RESULT_OK, intent);
Log.d(TAG, selectedNode.toString());
finish();
}
startActivityForResult() method receives an intent and requestCode, so you should change it to:
startActivityForResult(intent, REQUEST_CODE)
And onActivityResult() method is automatically called when returning from the activity, so you should remove the direct call you made to the method, i.e. onActivityResult(RESULT_OK, RESULT_OK, intent)
In android studio I have two activities, Main and Settings.
The settings activity is called from main activity and there is a button for logging out. When I click on this button the settings activity finishes and the main activity appears again. How can I make the main activity know that the log out button was clicked?
And what if there is many actions that I can perform in settings activity?I don't want to return result to main activity, I want to write that actions somewhere and read from there in Main activity.
In the MainActivity you can start the SettingActivity using
Intent intent = new Intent(this, SettingActivity .class);
startActivityForResult(intent, yourRequestCode);
after that in SettingActivity when you click the button logout, parse the boolean using
Intent intent= new Intent();
intent.putExtra("isClicked", true); // save clicked data
setResult(Activity.RESULT_OK, intent);
finish();
then, in MainActivity you can call onActivityResult like this
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == yourRequestCode) {
Boolean isLogoutClicked = data.getExtras().getBoolean("isClicked");
}
now you get the data from the setting activity when the button logout is clicked.
in mainActivity
Intent intent = new Intent(this, SettingActivity .class);
startActivityForResult(intent, 123);
than
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
try {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 123 && resultCode == RESULT_OK)
{
String requiredValue = data.getStringExtra("key");
}
} catch (Exception ex) {
Toast.makeText(Activity.this, ex.toString(),
Toast.LENGTH_SHORT).show();
}
}
and set thus into your setting screen
Intent intent = getIntent();
intent.putExtra("key", value);
setResult(RESULT_OK, intent);
finish();
and check if value have data or anything you past that its come from setting screen
I am writing a component that allows user to pick a location based on the place indicated by the location picker. One of the requirements is to send the LatLng object back from the map activity to the activity that called the former. The problem is that returned result code is always RESULT_CANCELLED, despite setting it explicitly to RESULT_OK. Here's the code:
Calling activity:
public void getLocationBtn(View view) {
Intent i = new Intent(this, PickLocationActivity.class);
startActivityForResult(i, 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
location = data.getParcelableExtra("location");
Log.d(TAG, "gotLocation: " + location);
}
if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Location not chosen", Toast.LENGTH_SHORT).show();
}
}
}
Called activity:
btnFind.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
centerLatLang = mMap.getProjection().getVisibleRegion().latLngBounds.getCenter();
Button doneBtn = findViewById(R.id.locationPickerDoneBtn);
doneBtn.setEnabled(true);
}
});
}
public void doneBtn(View view) {
Intent returnIntent = new Intent();
returnIntent.putExtra("location", centerLatLang);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
btnFind get the coordinates, doneBtn confirms user's choice and comes back to the previous activity.
I have already tried replacing Intent returnIntent = new Intent(); with getIntent(), but it didn't work; the returned bundle was null.
it happen when your activity is using singleTask launch mode. so i recommand if you have below line in your manifest activity tab please remove it.
android:launchMode="singleInstance"
I am trying to pass data backwards to an activity, however I can never get my onActivityResult function to be called. When I start the new activity, I create a new intent like regular
Intent intent = new Intent(this, NewLoanCost.class);
intent.putExtra("defaultsArray", jDefaultsArray.toString());
intent.putExtra("loanSelection", loanSelection);
intent.putExtra("buyerSellerSelection", buyerSellerSelection);
startActivity(intent);
and when I want to go back to the previous activity, I am overriding the back button to create a new intent and store the data
#Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("fullCosts", fullCosts.toString());
setResult(RESULT_OK, intent);
super.onBackPressed();
}
but in the first activity, I can't even get a debugging toast to appear. Am I missing something blatant?
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(this, "onActivityResult", Toast.LENGTH_SHORT).show();
if (requestCode == 1) {
if(resultCode == RESULT_OK){
try {
jDefaultsArray = new JSONArray(data.getStringExtra("fullCosts"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
To receive a result you should use startActivityForResult instead of startActivity
Change your code to
Intent intent = new Intent(this, NewLoanCost.class);
intent.putExtra("defaultsArray", jDefaultsArray.toString());
intent.putExtra("loanSelection", loanSelection);
intent.putExtra("buyerSellerSelection", buyerSellerSelection);
startActivityForResult(intent, 1);
the second parameter is an int with the request code you will use in onActivityResult
Use startActivityForResult.
Replace your code with this, then it should work:
Intent intent = new Intent(this, NewLoanCost.class);
intent.putExtra("defaultsArray", jDefaultsArray.toString());
intent.putExtra("loanSelection", loanSelection);
intent.putExtra("buyerSellerSelection", buyerSellerSelection);
startActivityForResult(intent);