Question

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

Was it helpful?

Solution 3

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.

OTHER TIPS

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

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top