4

I'm writting my own plug-in for an existing game engine (so to say it's 3rd-party lib in relation to the main application).

So, I have no access to the MainActivity sources.

Nevertheless I have to react somehow on main activity lifecycle events (onCreate, onDestroy, onPause, onResume, onNewIntent and some unimportant others).

Thanks to Application.ActivityLifecycleCallbacks, I have no problems with most of them.

The problem occurs with onNewIntent(). I can't find out a listener for this event and imagine a way to handle it.

Does anybody know how to catch onNewIntent event (surely, except overriding it)?

Soham
  • 4,397
  • 11
  • 43
  • 71
papirosnik
  • 311
  • 1
  • 3
  • 14

1 Answers1

5

onNewIntent() works for singleTop or singleTask activities which already run somewhere else in the stack. if the MainActivity is not declared with singleTop or singleTask attributes, even if you use below code, it won't work:

@Override //won't be called if no singleTop/singleTask attributes are used
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent);
    // ...    
}

To assure all setup logic hooked, it is best use onResume() by utilizing getIntent().

@Override
protected void onResume() { //will be called in any cases
    super.onResume();
    // getIntent() should always return the most recent        
}
David
  • 15,894
  • 22
  • 55
  • 66
  • Obtaining intent in onResume and comparing it with previous one sounds like a not bad idea. If intents are different I can do what I have to do for the new intent. A lag is admissible in my case. Accepted. ) – papirosnik Jul 22 '16 at 12:27
  • 1
    Did this work? According to the javadoc for `Activity.onNewIntent`: "Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent." – conca Jun 24 '20 at 22:25