0

I want to start a service from my main activity and pass an object with Serializable. However when I get the object in the Service and I call a method, it is null and I don't know why. Here is my MainActivity class:

public class MainActivity extends AppCompatActivity implements View.OnClickListener, Serializable
{
private transient SetVocabulary setVocabulary;
private transient Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setVocabulary = new SetVocabulary();
    findViewById(R.id.imageDizionario).setOnClickListener(this);
    findViewById(R.id.imageGruppi).setOnClickListener(this);
    findViewById(R.id.imageImpara).setOnClickListener(this);

    Intent intentService = new Intent(MainActivity.this, ServiceChiusuraApp.class);
    intentService.putExtra(Constants.codiceMainActivity,this);
    Bundle bundleService = new Bundle();
    bundleService.putSerializable(Constants.codiceMainActivity,this);
    intentService.putExtras(bundleService);
    startService(intentService);
}

public SetVocabulary getSetVocabulary()
{
    return setVocabulary;
}

@Override
public void onClick(View v)
{
    startActivity(intent);
}

}

Here is my Service class:

public class ServiceChiusuraApp extends Service
{
MainActivity mainActivity;
@Override
public IBinder onBind(Intent intent)
{
    mainActivity = (MainActivity) intent.getExtras().getSerializable(Constants.codiceMainActivity);
    return null;
}

@Override
public void onTaskRemoved(Intent rootIntent)
{
    super.onTaskRemoved(rootIntent);
    SharedPreferences sharedPref = mainActivity.getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putStringSet(Constants.codiceSetString,mainActivity.getSetVocabulary());
    stopSelf();
}
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Tosch
  • 17
  • 4

2 Answers2

0

I need to call mainActivity.getSetVocabulary()

In such case you don't want to serialize anything (especially Activity which is all kinds of wrong).

Instead, try binding Activity to Service. This will allow your Service and Activity to communicate. Please consult this question on how to call Activity class method from Service.

Siegmeyer
  • 4,312
  • 6
  • 26
  • 43
  • 1
    @Tosch an activity is not a regular class. You should not (try to) pass them around. The OS manages their lifecycle – Tim Aug 09 '17 at 15:44
  • `Activity` is managed by the operation system, as Tim pointed out. Serialization and consequent deserialization will result in making a copy of the original object, with all the references broken (OS will not by able to manage it anymore). Plus, serialization of an Activity is simply not possible. It's too complex object. – Siegmeyer Aug 09 '17 at 15:49
  • 1
    aah I saw with logs that the references are different as you said. This is why the method returns null! – Tosch Aug 09 '17 at 16:16
0

You want to return data from your Service to your Activity. There are several ways to do this:

  • Bind your Activity to your Service and using AIDL create a method on your Service that returns data.
  • Create a BroadcastReceiver in your Activity that listens for a specific Intent ACTION. In your Service, when you want to send data to the Activity,create an Intent with the appropriate ACTION, add the data as "extras", and call sendBroadcast().
David Wasser
  • 93,459
  • 16
  • 209
  • 274