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
}
Related
I have a hierarchy like this in my android app.
ProfileActivity
SettingTabFragment
ProfileContentFragment
From inside ProfileContentFragment I call another activity called ProfileVideoRecordingActivity using startActivityForResult()
Here is the code in ProfileContentFragment
Intent videoRecordIntent = new Intent(mActivity, ProfileVideoRecordingActivity.class);
startActivityForResult(videoRecordIntent, REQUEST_CODE_PROFILE_VIDEO_PATH);
Here is how I return from ProfileVideoRecordingActivity
Intent intent = new Intent();
intent.putExtra(VIDEO_PROFILE_PATH, renamedVideoFile.getAbsolutePath());
setResult(Activity.RESULT_OK, intent);
//Go back to calling Activity
finish();
Problem is neither ProfileActivity's nor ProfileContentFragment's onActivityResult() is called.
ProfileActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
}
SettingTabFragment
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
}
ProfileContentFragment
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE_PROFILE_VIDEO_PATH){
if(resultCode == Activity.RESULT_OK) {
String profileVideoPath = data.getExtras().getString(ProfileVideoRecordingActivity.VIDEO_PROFILE_PATH);
Log.d("DEBUG", profileVideoPath);
}
}
}
I tried solutions from stackoverflow where I need to call fragment's onActivityResult explicitly from activity but that didn't work.
Here is what I tried.
ProfileActivity inside onActivityResult()
List<Fragment> fragments = getSupportFragmentManager().getFragments();
if (fragments != null) {
for (Fragment fragment : fragments) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
SettingTabFragment inside onActivityResult()
List<Fragment> fragments = getChildFragmentManager().getFragments();
if (fragments != null) {
for (Fragment fragment : fragments) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
Both of them didn't work. What can I do?
MainActivity.java
LoginFragment _loginFragment = new LoginFragment();
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
_loginFragment.onActivityResult(requestCode, resultCode, data);
}
LoginFragent.java
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
}
At the top I have 1 main activity and a fragment to control my Facebook Login, it turn out that after pressing the button the apps crash on this two class.. How to solve this issues as the use of
android.support.v4.app.fragemt
is not an option for me.
i have a tab activity in which i have an activity and in that Activity i have a Fragment..
in tabActivity:-
TabActivity
{
#ovverride
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
}
}
in Activity:-
Activity
{
#ovverride
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
}
}
in Fragment:-
Fragment
{
void startActivityForResult()
{
startActivityForResult(intent,requestCode);
}
#ovverride
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
//doing my work here
}
}
now the problem is when i had used support Fragment then i do not need to have #ovverride onActivityResult in Activity or in TabActivity it was working very fine..but now i have android.app.Fragment and i have tried everything but i could not able to get OnActivityResult in my app Fragment...
please solve this..i'll be a great help..
try like this may help you,
Explicit call from fragment to onActivityResult function as follows
In Parent Activity class, override the onActivityResult() method and even override the same in Fragment Class and call as the following code.
In Parent Class:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.dualPane);
fragment.onActivityResult(requestCode, resultCode, data);
}
In fragment
#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);
}
}
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
}