2

My startActivityForResult is not returning to onActivityResult when the logout Preference is selected. The strange thing is that I'm pretty sure it used to work before I starting converting to using REST calls, but I don't know what might have been changed. MainOverview activity calls the Settings Activity. Yes, the onCreate method is omitted for MainOverview since I don't think it's important here. Here's the MainOverview code:

public class MainOverviewActivity extends BaseActivity implements OnClickListener { public void onClick(View v) { switch(v.getId()) { case R.id.show_settings: openSettings(); break; } } public static final String INTENT_RESULT_LOGOUT = "logout"; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Log.i("MainOverviewActivity", "onActivityResult before finish() :)"); if(requestCode == 123) { if(resultCode == Activity.RESULT_OK) { if(data.getBooleanExtra(SettingsActivity.INTENT_RESULT_LOGOUT, false)) { Log.i("MainOverviewActivity", "finish() :)"); Intent i = new Intent(this, LoginActivity.class); startActivity(i); finish(); } } } } protected void openSettings() { Intent intent = new Intent(this, SettingsActivity.class); startActivityForResult(intent, 123); } } 

Here's my Settings activity code:

public class SettingsActivity extends PreferenceActivity { public static final String INTENT_RESULT_LOGOUT = "logout"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent returnIntent = new Intent(); returnIntent.putExtra(INTENT_RESULT_LOGOUT, true); setResult(RESULT_OK, returnIntent); finish(); } 

Here's my settings.xml page, where I'm getting a "java.lang.NullPointerException at com.android.layoutlib.bridge.android.BridgeXmlBlockParser.next(BridgeXmlBlockParser.java:301)" on the preview page, and I have no idea why:

 <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="Server account"> <Preference android:title="Logout" android:key="logout" /> <Preference android:title="Version" android:key="version" /> </PreferenceCategory> </PreferenceScreen> 

It makes it to the onResume() within MainOverview, but not onActivityResult(), and I'm boggled as to why. Resultcode is above 0. Any suggestions would be great. I've read several other StackOverflow pages, and haven't seen an answer yet. I am not setting the launchMode anywhere. I'm out of ideas.

I've been working on it for a week and am still stuck.

Thanks in advance!

Devin

4
  • I looked link and noticed that getCallingActivity() does return null when the the preference is clicked. Somehow it's not retaining the fact that it was created by startActivityOnResult. Why is that? Commented Dec 28, 2013 at 23:15
  • Despite what the docs say, onResume is getting called when the activity returns, and onActivityResult is not getting called. I'm becoming more confused the more I investigate. Some more help/suggestions would be great! Commented Dec 28, 2013 at 23:48
  • What are you doing when the Preferences are clicked in SettingsActivity? Also, are you doing anything fancy in your AndroidManifest, like android:noHistory or android:launchMode? Commented Jan 4, 2014 at 1:48
  • noHistory or launchMode never gets set anywhere in my project. I'm currently not allowing any preferences to be clicked in SettingsActivity so as to simplify it and eliminate any bugs. Commented Jan 6, 2014 at 16:12

3 Answers 3

1

Replace

setResult(123, returnIntent); 

with

setresult(RESULT_OK, returnIntent); 

The result code is the first parameter - the 123 is known only to the calling activity

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for responding! I sure was hopeful, but I'm afraid that didn't fix it, though I still edited my code accordingly. My previous code did leave out a constant with value 123 that I was using in the parent activity. It just never makes it back to onActivityResult.
Instead it returns back to MainOverviewActivity.
0

I think you do not need to check the parent when you logout. Use the below insted.

public void logout() { Intent returnIntent = new Intent(); returnIntent.putExtra(INTENT_RESULT_LOGOUT, true); setResult(Activity.RESULT_OK, returnIntent); finish(); } 

7 Comments

The previous programmer put the parent check there, with no explanation as to why. Either way, every time I've stepped through it, it runs just the setResult call, not the getParent().
See my comment on my original post above for more insight.
@DevinCrane I think this is the issue, since the setResult() tells the activity that a startActivityForResult() was called. It should then go to the onActivityResult() automatically.
@epsilonDelta Thank you for your answer, but I don't understand. I'm calling setResult().
@DevinCrane Correct but by creating a new Intent by giving it parameters of classes, you are implying that you wish to start a new "chain" of switching between classes (it shows that you didn't do a startActivityForResult()). Also, make the change that Greg said below.
|
0

With some help, I finally discovered that the code was returning to onResume() in another class, which is where the LoginActivity was being created. It still doesn't ever return to onActivityResult, which doesn't make sense to me, but it at least works again.

Thanks again for all your help! I learned a good bit from your suggestions.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.