How to pass same class method inside onActivityResult() in android? - android

I have one method - addEntryData. I want to pass the addEntryData method inside onActivityResult.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent );
// check if the request code is same as what is passed here it is 2
if(requestCode==2) {I want ot put my addEntryData method here.}
}
public void addEntryData(SMSForwardEntry smsForwardEntry)
{
smsForwardEntries.add(smsForwardEntry);
smsForwardAdapter.notifyDataSetChanged();
saveData();
Analytics.track(AnalyticsEvents.SMS_FORWARD_ADDED);
}

you need to use startActivityForResult()
Example:
Intent i = new Intent(this, SecondActivity.class);
// add data that you want to pass to other activity in to Intent
startActivityForResult(i, LAUNCH_SECOND_ACTIVITY);
and in SecondActivity you can get same Intent using getIntent().
And from SecondActivity return result using:
setResult(RESULT_OK); .....
and handle it in onActivityResult()

#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent );
// check if the request code is same as what is passed here it is 2
if(requestCode==2) {
addEntryData(smsForwardEntry);//Add like this
}
}
public void addEntryData(SMSForwardEntry smsForwardEntry) {
smsForwardEntries.add(smsForwardEntry);
smsForwardAdapter.notifyDataSetChanged();
saveData();
Analytics.track(AnalyticsEvents.SMS_FORWARD_ADDED);
}

Related

Make an activity A wait an activity B finish to continue

I have an activity A with this fuction:
public void onSettingsClick(View view){
Intent intent = new Intent(this, Settings.class);
startActivity(intent);
checkSettings();
}
Is there any way to make the activity A to wait for the activity B ("Settings.class") finish to execute the fuction "checkSettings();" ?
Thanks
Edit: I may have misunderstood your question. If you want to run checkSettings() function in B then you need to define and call that function in your B activity.
Otherwise, if you want to wait for activity B to end before running checkSettings() then copy the following code.
In A:
public void onSettingsClick(View view){
Intent intent = new Intent(this, Settings.class);
startActivityForResult(intent, 1);
}
then also in A Override onActivityResult method.. this gets called when B ends:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
checkSettings();
}
In your Activity A write
public void onSettingsClick(View view){
Intent intent = new Intent(this, Settings.class);
startActivityForResult(intent, 100);
}
and also in you Activity A override onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
if(requestCode==100){
checkSettings();
}
}
}
and in your Activity B
when you want to finish that activity write that piece of code there
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
finish();
Another approach to doing this since you are launching a separate activity is call check settings in onResume. This way since you're not having to pass any data and if the are other reasons you may want to refresh or recheck your settings, it will do so any time the activity is brought back to the top.
override fun onResume() {
super.onResume()
checkSettings()
}

onActivityResult not getting called from fragments

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
}

startActivityForResult From Fragment Not Working

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);
}
}

onActivityResult not being called inside FragmentActivity that contains Android Map view fragment

I have a fragment activity that displays an Android V2 Map. Inside I also have a onActivityResult used to handle the intent Extras that needs to be passed from the calling activity
public class DisplayMap extends FragmentActivity implements LocationListener {
#Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.map);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
Log.v("TEST", "********************************************");
}
}
Here is the code form the activity that calls it.
Intent i = new Intent("com.example.DisplayMap");
setResult(RESULT_OK, i);
startActivityForResult(i, 2014);
But somehow the onActivityResult is not called inside.
Thanks in advance.
Dennis
onActivityResult() needs to be in the calling activity, it retrieves the result, as the name suggests.
To return a result from the called activity you'll need to use setResult() and finish that activity:
called activity:
Intent i = new Intent();
setResult(RESULT_OK, i);
finish();
with RESULT_OK being passed as the resultCode parameter to onActivityResult() and i as intent
calling activity:
Intent i = new Intent("com.example.DisplayMap");
startActivityForResult(i, REQUEST_CODE);
and to receive the result:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(requestCode == REQUEST_CODE){
if (resultCode == RESULT_OK){
// RESULT OK, take the returned extras from intent and use them
}
}
}

onActivityResult not called in fragment

On my fragment I am doing:
Intent intent = new Intent(getActivity(), OtherActivity.class);
startActivityForResult(intent, RETURN_CODE);
I have overriden onActivityResult on my Activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
logger.debug("!!ACTIVITY!!!Returned from the activity!!!!");
super.onActivityResult(requestCode, resultCode, data);
}
The same goes for the fragment! The problem is that this method is only being called on the Activity and not in the fragment. Any clue why?
there is an issue with support v4, try using onPostResume
in onActivityResult in Activity use following code
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
returningWithResult = true;
this.mData = data;
mRequestCode = requestCode;
mResultCode = resultCode;
}
and in onPostResume
#Override
protected void onPostResume() {
// TODO Auto-generated method stub
super.onPostResume();
if (returningWithResult)
<your fragment>.actionOnActivityResult(mRequestCode, mResultCode, mData);
returningWithResult = false;
}
also trying launching intent for result from activity only
public void launchIntent(Intent intent, int code) {
startActivityForResult(intent, code);
}
In Fragment use
getActivity().launchIntent(intent, PICK_FROM_CAMERA);
I dont know the reason, but it worked for me. Hope it helps.

Categories

Resources