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

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

Related

How to pass same class method inside onActivityResult() in 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);
}

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
}

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.

Third activity called returns its result to the first activity instead of sending the result to the second activity?

Scenario:
First activity starts seconds activity with startActivityForResult
Second activity starts third activity with startActivityForResult
Expected result:
Third (last) activity sets a result which is caught on the second activity's onActivityResult
Current result:
Third (last) activity sets a result which is caught on the first activity's onActivityResult
How can I set a result on the third activity which will be caught on the second activity's Third (last) activity sets a result which is caught on the second activity's onActivityResult?
Some code snippets:
First activity
public class TestProjActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
Log.i("TAAAG", "1st activity - startActivityForResult");
Intent intent = new Intent(TestProjActivity.this, Activ2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivityForResult(intent, 1008);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("TAAAG", "1st activity - onActivityResult");
if (resultCode == RESULT_OK) {
Log.i("TAAAG", String.valueOf(requestCode));
switch (requestCode) {
case 1008:
String info = data.getExtras().getString("KEY1");
Log.i("TAAAG", "1st activity - onActivityResult - printing result");
Log.i("TAAAG", info);
break;
}
}
}
}
Second activity
public class Activ2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("TAAAG", "2nd activity - startActivityForResult");
Intent intent = new Intent(Activ2.this, Activ3.class);
startActivityForResult(intent, 1009);
Intent intent2 = new Intent();
intent2.putExtra("KEY1", "VALUE1");
setResult(RESULT_OK, intent2);
finish();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("TAAAG", "2nd activity - onActivityResult");
if (resultCode == RESULT_OK) {
Log.i("TAAAG", String.valueOf(requestCode));
switch (requestCode) {
case 1009:
String info = data.getExtras().getString("KEY2");
Log.i("TAAAG", "2nd activity - onActivityResult - printing result");
Log.i("TAAAG", info);
break;
}
}
}
}
Third activity
public class Activ3 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("TAAAG", "3rd activity - Inserting Value and finishing");
Intent intent = new Intent();
intent.putExtra("KEY2", "VALUE2");
setResult(RESULT_OK, intent);
finish();
}
}
use Activity flags to forward result to super activity:
intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
I think You Should Remove finish(); in oncreate last line in Second activity....

Categories

Resources