Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to set a listener for Acivity.onNewIntent()?

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

like image 642
papirosnik Avatar asked Sep 02 '25 06:09

papirosnik


1 Answers

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        
}
like image 101
David Avatar answered Sep 04 '25 19:09

David