I found the solution.
First of all, I discover that...
you cannot use the admin_notices
hook inside the activation hook function. More details here
Thanks the link above, I found this solution :
class MyAddon {
const ACTIVATION_ERROR = "foo-activation-error"
public function __construct(){
register_activation_hook( __FILE__, array( $this, 'install' ) );
if( is_admin() ){
add_action( "admin_init", array( $this, "init" ), 2 ); //priority 2 to load after the plugin
}else{
add_action( "init", array( $this, "init" ), 2 );
}
}
public function init(){
$has_to_be_deactivate = get_transient( self::ACTIVATION_ERROR );
if( is_admin() ){
if( $has_to_be_deactivate ){
add_action( 'admin_notices', array( $this, "display_warning_no_activation" ) );
deactivate_plugins( plugin_basename( __FILE__ ) );
}
}
}
public function install(){
if ( is_plugin_inactive( 'MainPlugin.php' ) ){
set_transient( self::ACTIVATION_ERROR , true );
}
public function display_warning_no_activation() {
if( get_transient( self::ACTIVATION_ERROR ) ){
?>
<div class="notice notice-error" >
<p><?php echo esc_html__("Impossible to activate the addon : « My Addon », please check if the plugin « My Main Plugin » is active!", "addon-domain" ); ?></p>
</div>
<?php
}
delete_transient( self::ACTIVATION_ERROR );
}
}
}