onActivityResult not called in fragment - android

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.

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

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
}

android onActivityResult not comming to android.app.Fragment

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
}

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

android - How to use startActivityForResult() in tabs

My application have 3 tabs. In this one tab have multiple activities(means this tab have navigation to the child tabs). I used the startActivityforResult() in one child activity. But control never goes to onActivityResult() method. How to implement this. please can anybody help me.
code
public class Activity_1 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
View contentView = LayoutInflater.from(getParent()).inflate(R.layout.static_search_filters, null);
setContentView(contentView);
states_tv = (TextView)findViewById(R.id.state);
states_tv.setOnClickListener(states_etListener);
}
private OnClickListener states_etListener = new View.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(getParent(), RB_CategoriesMList.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intent,GET_SEL_STATES_LIST);
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
//I do some stuff here
}
}
//Activity_2
public class RB_CategoriesMList extends ListActivity
{
public Button sbtn;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.categories_list);
sbtn =(Button)findViewById(R.id.submit_categories);
sbtn.setOnClickListener(sbtn_listener);
}
private OnClickListener sbtn_listener = new View.OnClickListener()
{
public void onClick(View v)
{
Intent state_intent = getIntent();
state_intent.putExtra("selected_states", "");
setResult(RESULT_OK,state_intent);
finish();
}
};
}
Dont call
super.onActivityResult(requestCode, resultCode, data);
When you receive the onActivityResult, check if the request code is your and then do your stuff. In the other cases you should call the super.
So:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == GET_SEL_STATES_LIST) {
// Do your stuff
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
getParent().startActivityForResult(yourNewIntent, yourResult);

Categories

Resources