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)?

1 Answer 1

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        
}
2
  • 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, 2016 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, 2020 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.