How to pass the request code to onSearchRequested like startForActivityResult? - android

I want to pass a request code to a searchable activity, like startActivityForResult. But onSearchRequested and startSearch cannot be passed it. In below example, I assume that after pressing a menu icon, a Search dialog will be shown at the top of window and I will send the request code and query to the searchable activity.
private static final int SEARCH_REQUEST_CODE = 100;
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
switch (requestCode) {
case SEARCH_REQUEST_CODE:
// Something to do after finishing the searchable activity
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId()) {
// ...
case 2:
onSearchRequested(); // I want to pass SEARCH_REQUEST_CODE here!
return true;
// ...
}
}
I have read a tutorial of Search Interface in API Guides ( https://developer.android.com/guide/topics/search/search-dialog.html ) and the API reference. But, I cannot find out how to do this.

Related

viewPager.setCurrentItem() doesn't work in onActivityResult

I have implemented FragmentStatePagerSupport. I have 142 pages. I have also an activity that is MyListActivity and has ListView. When I click ListView item, it returns a page number. I want to use this page number for viewPager current page. viewPager.setCurrentItem() doesn't work in onActivityResult.
Here is how I call startActivityForResult:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu1:
Intent intent = new Intent(this, MyListActivity.class);
startActivityForResult(icindekiler, 0);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And here is the listView of MyListActivity:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListViewContent selectedItem = (ListViewContent) parent.getItemAtPosition(position);
int pageNo = book.getSpine().getResourceIndex(selectedItem.getResource().getHref());
Intent data = new Intent();
data.putExtra("PageNo", pageNo); // pageNo: 0 or 1 or 2 or ... or 142
setResult(0, data);
finish();
}
});
onActivityResult method in FragmentPageStateSupport activity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == 0) {
if (data.hasExtra("PageNo")) {
pageNo = data.getExtras().getInt("PageNo");
Toast.makeText(getBaseContext(), "pageNo is " + pageNo, Toast.LENGTH_SHORT).show();
viewPager.setCurrentItem(pageNo);
}
}
}
For example pageNo is 11, Toast says "pageNo is 11" but viewPager doesn't change. When I use debugging in Android Studio, application runs Looper.java. I try viewPager.setCurrentItem(11) on different method (for example button click), this time it is working.

OnBackPressed multiple instances of same activity

I am having an activity with custom list view. In my list view there are two types of items, with different layout. On items there is a switch and when I turn off the switch I go to Activity A, and when I turn on the switch I go to Activity B.
Here is my code:
Switch enable = (Switch) rowView.findViewById(R.id.enable);
enable.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Intent intentChange = new Intent(getContext(), ActivityA.class);
intentChange.putExtra(PROPERTY, "test1");
((Activity)getContext()).startActivityForResult(intentChange, 100);
} else {
Intent newIntent=newIntent(getContext(),ActivityB.class);
((Activity)getContext()).startActivityForResult(newIntent, 200);
}
}
});
In each of these activities I call this:
Intent changedIntent=new Intent();
changedLimitIntent.putExtra("changed",changed);
changedLimitIntent.putExtra("changedDesc","desc);
setResult(Activity.RESULT_OK,changedIntent);
finish();
In my activity with custom adapter I have this code:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == ActivityA.REQUEST_CODE) {
....
} else if (requestCode == ActivityB.REQUEST_CODE) {
.....
}
}
}
This work OK for me, however, when I receive the result from Activity A or Activity B on the activity with custom list view, and when I click back button is how I have this activity as many times I wait for result from other activities on back stack. I don't know what the problem is. I don't like to have my activity as many times I wait for result from other activities.
I hope I was clear with my question.
Try this to launch new activity. Hope it will help you.
Activity_A.this.finish();
Intent intentSettings = new Intent(Activity_A.this, Activity_B.class);
intentSettings.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentSettings);

Calling Activity from Fragment then return to Fragment

I have an app that has a few tabs. These tabs are all fragments. On the first tab fragment, I have a text view and a button, which I press on to call an activity.
This activity displays a list of items, car names.
I want to be able to click on a car in the list and return back to the calling fragment and update the text view with the car name I selected.
Can anyone help me out with this?
startActivityForResult() is probably what you're looking for. So a quick example (making super-basic assumptions about your data structure -- substitute as required) would be to make your fragment override onActivityResult(), define a request code, and then start the activity using that request code:
// Arbitrary value
private static final int REQUEST_CODE_GET_CAR = 1;
private void startCarActivity() {
Intent i = new Intent(getActivity(), CarActivity.class);
startActivityForResult(i, REQUEST_CODE_GET_CAR);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// If the activity result was received from the "Get Car" request
if (REQUEST_CODE_GET_CAR == requestCode) {
// If the activity confirmed a selection
if (Activity.RESULT_OK == resultCode) {
// Grab whatever data identifies that car that was sent in
// setResult(int, Intent)
final int carId = data.getIntExtra(CarActivity.EXTRA_CAR_ID, -1);
} else {
// You can handle a case where no selection was made if you want
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
Then, in the CarActivity, wherever you set a click listener for your list, set the result and pass back whatever data you need in an Intent:
public static final String EXTRA_CAR_ID = "com.my.application.CarActivity.EXTRA_CAR_ID";
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Assuming you have an adapter that returns a Car object
Car car = (Car) parent.getItemAtPosition(position);
Intent i = new Intent();
// Throw in some identifier
i.putExtra(EXTRA_CAR_ID, car.getId());
// Set the result with this data, and finish the activity
setResult(RESULT_OK, i);
finish();
}
call startActivityForResult(theIntent, 1);
In the activity started, once the user selects a car, make sure to put the car in an intent and set the result of the activity to that intent
Intent returnIntent = new Intent();
returnIntent.putExtra("result", theCar);
setResult(RESULT_OK, returnIntent);
finish();
Then, in your fragment, implement onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result = data.getStringExtra("result");
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
} //onActivityResult
Make Sure to override onActivityResult() in the fragment's hosting activity too, and call the super
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
This is because the parent activity hijacks the onActivityResult method, and if you don't call super() then it wont get passed to the fragment to handle it

back button error, action down

I have a problem with my code about back button. I've tried a lot of answers here in this site.
I have the mainActivity that calls a second activity with startActivityforResult. This second activity starts bluetooth and show a list of the bonded devices, but if I press the backbutton it stops the app with an error.
public class Main extends Activity implements OnSeekBarChangeListener{
...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String address = data.getExtras().getString(BondedDevices.DEVICE_ADDRESS);
if (resultCode==Activity.RESULT_CANCELED){
Toast.makeText(getApplicationContext(), "Finalizando dispositivos pareados", Toast.LENGTH_SHORT).show();
return;
}
switch (requestCode) {
case DISPOSITIVOS_PAREADOS:
if(resultCode==Activity.RESULT_OK){
mConnectThread = new ConnectThread(address);
mConnectThread.start();
estado = EST_CONECTADO; //informa que esta conectado
atualizaEstado();
break;
}
return;
}
}
But when I am in the second activity, and try to back to the mainactivity just pressing the back button, I get an error on main activity and my app return an error:
public class BondedDevices extends ListActivity {
....
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
if(D) Log.e(TAG, "+++ ON BACK PRESSED +++");
setResult(Activity.RESULT_CANCELED);
this.finish();
}
or like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
return true;
}
}
return false;
}
I have tried a lot of different code, but it still doesn't work. Please someone help me. Thank you.
you should call super.onBackPressed();
like this
#Override
public void onBackPressed() {
super.onBackPressed();
if(D) Log.e(TAG, "+++ ON BACK PRESSED +++");
setResult(Activity.RESULT_CANCELED);
this.finish();
}
When you return from the second Activity, you have not set an Intent as the data for the result in the back button case, yet the first thing you do before checking the value of resultCode for RESULT_CANCELED is try to obtain the address from the Intent, which is null so this will cause a NullPointerException.
You need to reorder the lines in your onActivityResult() to look more like this:
if (resultCode==Activity.RESULT_CANCELED){
Toast.makeText(getApplicationContext(), "Finalizando dispositivos pareados", Toast.LENGTH_SHORT).show();
return;
}
//Do this after checking for cancel
String address = data.getExtras().getString(BondedDevices.DEVICE_ADDRESS);
/* The rest of the existing code */
Also, you should NOT override BOTH the onBackPressed() and onKeyDown(), stick with one of them. You're only causing confusion about which code path gets called first. Even in the case where the result is set with a blank Intent, you will still get a NullPointerException in your existing code because the extras bundle of that Intent will still be null.

Android: Simple way to call phone book and return number

I'm really new in Android programming, so I have a simple question getting a phone number from contacts in my application. The method I want to implement is that the user clicks a button in my app and will be forwarded to the contacts. In contacts user has to click on a phone number and this number should be returned to a textfield. Is there a simple way to do this instead to implement it by ContentResolver?
Thanks for your answer.
Launch a a Pick contacts intent with startActivityForResult. In your case you just add the startActivityForResult call in the OnClick listener for the button.
public class MyActivity extends Activity {
static final int PICK_CONTACT_REQUEST = 0;
protected boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
// When the user center presses, let them pick a contact.
startActivityForResult(
new Intent(Intent.ACTION_PICK,
new Uri("content://contacts")),
PICK_CONTACT_REQUEST);
return true;
}
return false;
}
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
// A contact was picked. Here we will just display it
// to the user.
startActivity(new Intent(Intent.ACTION_VIEW, data));
}
}
}
}

Categories

Resources