Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7504905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:43:51+00:00 2026-05-29T21:43:51+00:00

I have an Activity that starts and binds to a Service . I then

  • 0

I have an Activity that starts and binds to a Service. I then have another Activity, launched from the first. Upon returning to the first Activity from the second, I need to invoke a method of the Service (saves some data).

While viewing each Activity, my Activity lifecycle methods appear to cope adequately with screen orientation changes, provided I return to the same screen orientation before exiting the Activity.

The problem happens when I return to the first Activity with a different orientation from when I left it. If that happens, I lose my reference to my Service and consequently run into a NullPointerException in onActivityResult(). So if I launch my second Activity in portrait mode, switch to landscape while viewing the second Activity, and return to the first Activity in landscape mode, it’ll crash.

What might I be missing? I don’t want to use the manifest file to indicate I’ll handle configuration changes – strikes me as a somewhat ugly hack which doesn’t address the main problem. Unless I’m missing something again…

Here are extracts from my lifecycle methods from the first Activity:

@Override
protected void onStart()
{
super.onStart();

// start and bind to service
startService(smsIntent);
connection = new SMServiceConnection();
bindService(smsIntent, connection, Context.BIND_AUTO_CREATE);

}

@Override
protected void onRestart()
{
super.onRestart();
}

@Override
protected void onResume()
{
super.onResume();
}

@Override
protected void onPause()
{
super.onPause();
sms.save(); // autosave
}

@Override
protected void onStop()
{
super.onStop();
unbindService(connection);
// stopService(smsIntent); //doesn't appear to have any effect
}

@Override
protected void onDestroy()
{
super.onDestroy();
}

EDIT: Here are extracts from my SMServiceConnection class, which is a private inner class in my Activity, which extends from a custom ServiceConnection class.

@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
    super.onServiceConnected(name, service);
    msg("Service connected");
    sms = getSMService();
    if (sms != null)
    {
    String s = sms.getStuff(); //etc.; haven't listed every method invoked on sms

        sms.saveSurvey();
    }
    } else
    {
    System.out.println("sms is null!");
    }

}

@Override
public void onServiceDisconnected(ComponentName name)
{
    super.onServiceDisconnected(name);
    msg("Service disconnected");
}

My ServiceConnection superclass is something like:

public class MyServiceConnection implements ServiceConnection
{
    private boolean serviceAvailable = false;
    private SMService sms;

    public void onServiceConnected(ComponentName name, IBinder service)
    {
        serviceAvailable = true;
        LocalBinder b = (LocalBinder) service;
        sms = b.getService();
    }

    public void onServiceDisconnected(ComponentName name)
    {
        serviceAvailable = false;
    }

    public boolean isServiceAvailable()
    {
    return serviceAvailable;
    }

    public SMService getSMService()
    {
    return sms;
    }


    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T21:43:53+00:00Added an answer on May 29, 2026 at 9:43 pm

    Your problem might be that onActivityResult() is called prior to the service being binded again when you return, which happens slightly after the bindcall in onstart.

    I would try one of two things:

    1. Try saving the data as pending information:

      if (sms == null) {
          mPendingResultCode = resultCode;
          mPedingResultData = new Bundle(intent.getExtras()); } else {
          handleData(resultCode, intent.getExtras()); }
      

      And then later in onServiceConnected call
      handleData(mPedingResultCode, mPedingResultData) if for example
      mPendingResultData != null

      And make sure to unset mPendingResultCode and mPendingResultData or
      some other indicator when you are done with the data.

    2. I’m unsure of this, but would perhaps try add the data handling to the back of the event queue by doing something like this in onActivityResult:

      final Bundle data = new Bundle(intent.getExtras);
      new Handler().postRunnable(new Runnable() {
          public void run() {
              "do some stuff with data and resultcode (which should be final in the parameter list anyway)"
          }
      }
      

    As you said you should not change the manifest to not recreate on orientation changed. It is an ugly hack that does not solve the problem and I don’t like when that is suggested. If you do that you still risk the same thing happening if some other configuration has changed such as language or if the activity has been destroyed temporarily by the framework to save resources.

    configchanges in manifest must only be used if you want to get callbacks on the change rather than recreate for a good. reason. Laziness is not one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an single Activity application that starts a Service and binds to it.
I have an activity (first) that starts a new activity (second). If I returned
So i have a service, that starts an activity displayed as a Popup thank's
I have an Activity that starts a service. When the activity is closed, I
I have an activity that starts a service. There is also a singleton Data
I have an Activity that starts a Service. In my Activity: startService(new Intent(this, MyService.class));
I have an activity that binds to a service. The service provides the functions
I currently have an Activty that when created starts and binds with a service
I have an activity that starts a service like that: Intent youtubeIntent = new
I have an Activity (called StartActivity) that starts a Service. The Service calls methods

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.