I have one checkbox which creates a Bluetooth connection. The problems is that when the Bluetooth permission request dialog box appears and I choose No, the checkbox still remains checked.
How can I get the requestcode from this activity, and put the checkbox to off if I get RESULT_CANCELED?
CheckBox turnBtOnOff=(CheckBox)findViewById(R.id.checkBox1);
turnBtOnOff.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
{
if(!mBluetoothAdapter.isEnabled())
{
Intent enableBtIntent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
//myAddress=mBluetoothAdapter.getAddress();
//Toast.makeText(getBaseContext(), myAddress, Toast.LENGTH_SHORT).show();
}
}
else
{
if(mBluetoothAdapter.isEnabled())
{
mBluetoothAdapter.disable();
}
}
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode ==REQUEST_ENABLE_BT) {
if (resultCode == RESULT_CANCELED) {
turnBtOnOff.setSelected(false);
}
}
}
protected void onActivityResult(int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_ENABLE_BT: {
if (resultCode == RESULT_CANCELED) {
......
}
break;
}
}
}
try this :
private static int RESULT_OK=111;
private static int PICK_CONTACT_REQUEST=112;
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
// ....do your code here
//
}
}
}
good luck
you need to override onActivityResult to get results back from an activity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//if 12345 is the int value you used to start bluetooth activity then
if(requestCode == 12345){
if(resultCode == RESULT_OK){
//do whatever you want
}
else{
//do whatever you want
}
}
}
This is a Little old, but I had the same problem today, this is my solution working on android 4.4.2
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode==RESULT_FIRST_USER){
//custom request is BLUETOOTH_REQUEST_DISCOVERABLE
if(requestCode==BLUETOOTH_REQUEST_DISCOVERABLE){
//Do something
}
}
}
the result is not RESULT_OK, because its not an operation to perform, its a user-defined acitivty.
Override the onActivityResult() method:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
//check for the requestcode here and check/uncheck the checkbox
}
Related
im using tabbaractivity and one of the tabs contains listView
i want to update the listView every time that i adding object.
to add object i do it from new activity
what i wants is when this activity will close the listView will update by using
adapter.notifydatachanged
in my activity that contains the tabs
if (id == R.id.addButton) {
Intent intent = new Intent(rootViewTabBarActivity.this, addPictureActivity.class);
startActivityForResult(intent,101);
return true;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
in the fragment (one of the tabs)
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((requestCode == 101) && (resultCode == Activity.RESULT_OK))
adapter.notifyDataSetChanged();
}
and the activity that create the object and save him to database
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAM_REQUEST) {
Bitmap btmp = (Bitmap)data.getExtras().get("data");
takenPhoto.setImageBitmap(btmp);
}
}
#Override
protected void onDestroy() {
super.onDestroy();
setResult(Activity.RESULT_OK);
}
Thanks
fragment not called to onActivityResult
You miss passing the data from Activity to fragment
In Activity, your code should be like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
YourFragment fragment = getFragment();
if (fragment != null) {
// Pass data to Fragment
fragment.onActivityResult(requestCode, resultCode, data);
}
}
}
public YourFragment getFragment() {
// You can find your fragment (by tag/ by id) here depends on your layout
}
I have a problem with the response of my activity. It is a module to react natively. I have this react class and I am trying to get result from my activity. I've tried catching the requestCode like this:
public CameraOpenerModule(ReactApplicationContext reactContext) {
super(reactContext);
getReactApplicationContext().addActivityEventListener(new ActivityEventListener() {
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.e("TEST", "listener start event");
if (requestCode == 9999) {
Log.e("TEST", "request code ");
}
}
});
}
and I open the class intent:
private void openCameraIntent(){
cameraIntent = new Intent(getReactApplicationContext(), CameraHelpActivity.class);
// cameraIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
cameraIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getReactApplicationContext().startActivityForResult(cameraIntent, 9999,null);
}
and another activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setResult(1111);
Log.i("ACTIVITY", "finish !!!!!");
finish();
}
Everything is ok. But I'm not catching the request code. Does anybody know why? Thanks
Add to MainActivity.java
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(mReactInstanceManager != null){
mReactInstanceManager.onActivityResult(requestCode,resultCode,data);
}
}
i have this fragment which makes a call to an activity using start activity for result. this activity is supposed to show a lock pattern. the problem is that the 'onActivityResult' is never called. I put some toasts to check but it never gets printed.
public class int_Create_Pattern extends Fragment {
private static final int REQ_CREATE_PATTERN = 1;
#Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
LockPatternView.MATRIX_WIDTH = 4;
Intent intent = new Intent(LockPatternActivity.ACTION_CREATE_PATTERN,
null, getActivity().getBaseContext(), LockPatternActivity.class);
startActivityForResult(intent, REQ_CREATE_PATTERN);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(resultCode, resultCode, data);
switch (requestCode) {
case REQ_CREATE_PATTERN: {
if (resultCode == LockPatternActivity.RESULT_OK) {
char[] pattern = data
.getCharArrayExtra(LockPatternActivity.EXTRA_PATTERN);
DataBaseHandler handler = new DataBaseHandler(getActivity()
.getApplicationContext());
handler.open();
String PatternToWrite = new String(pattern);
handler.createPattern(PatternToWrite);
handler.close();
Log.d("DEBUG", new String(pattern));
Toast.makeText(getActivity().getApplicationContext(),
"Pattern Recorded", Toast.LENGTH_LONG).show();
}
if (resultCode == LockPatternActivity.RESULT_CANCELED) {
Toast.makeText(getActivity().getApplicationContext(),
"Pattern Cancelled", Toast.LENGTH_LONG).show();
}
break;
}// REQ_CREATE_PATTERN
}
}
}
onActivityResult should be an Activity method, and not be on a Fragment.
Another problem is that you shouldn't call a new Activity from a Fragment. Implement an Interface for comunication, as described by the link bellow
http://developer.android.com/training/basics/fragments/communicating.html
The activity that holds your fragment should be the responsable for calling the new activity, and processing the result.
In Parent Class: do this code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.dualPane);
fragment.onActivityResult(requestCode, resultCode, data);
}
In Child Class:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//in fragment class callback
}
I have been fighting with this problem for hours now and have looked at pretty much every other post about this issue but can't find where I am going wrong. Please let me know if I am missing something simple.
Activity 1 ( hosts a CreateFragment)
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
((CreateFragment) getFragmentManager().findFragmentByTag(CreateFragment.TAG)).resultReturned(requestCode, resultCode, data);
}
CreateFragment:
private void chooseContact() {
Intent chooseContactInappIntent = new Intent( getActivity(), ContactPickerActivity.class );
getActivity().startActivityForResult( chooseContactInappIntent, PICK_CONTACT_REQUEST );
}
public void resultReturned( int requestCode, int resultCode, Intent data ) {
switch ( requestCode ) {
case PICK_CONTACT_REQUEST:
if ( resultCode == Activity.RESULT_OK ) {
//Do Cool Things
default:
break;
}
}
ContactPickerActivity:
#Override
public void onContactSelected( Object data) {
Intent returnIntent = new Intent();
returnIntent.putExtra("data",data);
setResult(RESULT_OK,returnIntent);
finish();
}
Activity One hosts a CreateFragment, when the use clicks a button in the CreateFragment the fragment calls the chooseContact() method which starts a new ActivityForResult. The ContactPickerActivity then displays the user a list of contacts. When the user selects a contact the onContactSelected method is called creating a new intent to pass back the selected data and then calls finish. I thought this would either call the onActivityResult in the base activity or the CreateFragment, but neither are called.
Any Ideas?
Thanks,
Nathan
In Fragment class work fine if you call at MainActivity onActivityResult override method and you don't need to changes inside the fragment class.
So
In Fragment that calls startActivityForResult like this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && (requestCode == GALLERY_REQUEST || requestCode == CAMERA_REQUEST)) {
// its not complicated .....
}
}
In MainActivity Class are must to call onActivityResult Also.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
My application starts with InitActivity that checks for login status, calls LoginActivity accordingly before proceeding to some logic.
I know I can use the startAcivityForResult() and onActivityResult() to ensure LoginActivity completes before doing doSomeMainLogic(), but my if check throws a curve in it. If I do this:
public class InitActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!userIsLoggedIn) {
Intent intent = new Intent("com.example.myapp.LOGINACTIVITY");
startActivityForResult(intent,1);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
doSomeMainLogic();
}
}
}
}
how do I get doSomeMainLogic() to fire if the user is already logged in?
Thanks much.
public class InitActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!userIsLoggedIn) {
Intent intent = new Intent("com.example.myapp.LOGINACTIVITY");
startActivityForResult(intent,1);
}
else {
doSomeMainLogic(); // this part is added
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
doSomeMainLogic();
}
}
}
}