"It seems that, if backup related process is involved, Application's
onCreate will not be executed!"
You are actually right based on your statement and the reason for that was clearly documented on android docs.
Android provides two ways for apps to back up their data:
Auto backup for apps and Key/Value Backup.
Both ways makes use of bmgr tool and basically what Auto backup does is same as what you did.
c:\yocto>adb shell bmgr restore com.yocto.wenote
Custom Application class does not exist after restore, Why is it so?
The docs clearly states that
During auto backup and restore operations, the system launches the app
in a restricted mode to both prevent the app from accessing files that
could cause conflicts and let the app execute callback methods in its
BackupAgent. In this restricted mode, the app's main activity is not
automatically launched, its Content Providers are not initialized, and
the base-class Application is instantiated instead of any subclass
declared in the app's manifest.
Even while your app is completely restored using bmgr tool it can still be under restricted mode (without its Custom Application class available but an instance of base-class Application).
Referencing your Custom Application class at this state or any method in it from anywhere in your app would surely return a null reference because it does not exist yet in your app due to the statement above.
You are expected to bring the app back to its default state by killing it entirely and re-starting it again, which is the one last thing Auto backup does behind the scene that you are not doing via your commands. This simply means your command statements wasn't completed before you re-launched the app.
--Kill app process and restart app
c:\yocto>adb shell am force-stop com.yocto.wenote
c:\yocto>adb shell monkey -p com.yocto.wenote 1
Below is my testcase based on your code using Android Studio IDE and a device with Android O
Add log inside Custom Application class onCreate
Log.d("MyApplicationLog", "MyApplication --> " + MyApplication.intstance());
Add log inside Launcher Activity class onCreate
Log.d("MainActivityLog", "MyApplication --> " + MyApplication.intstance());
Command 1
--Configure backup transport
c:\me\MyWebApp>adb shell bmgr transport android/com.android.internal.backup.LocalTransport
Output
Selected transport android/com.android.internal.backup.LocalTransport (formerly com.google.android.gms/.backup.BackupTransportService)
Command 2
--Backup app
c:\me\MyWebApp>adb shell bmgr backupnow com.android.webviewapp
Output
Running incremental backup for 1 requested packages.
Package @pm@ with result: Success
Package com.android.webviewapp with progress: 512/1024
Package com.android.webviewapp with progress: 1536/1024
Package com.android.webviewapp with progress: 2048/1024
Package com.android.webviewapp with progress: 2560/1024
Package com.android.webviewapp with result: Success
Backup finished with result: Success
Click app manually on launcher or run the monkey command which is synonymous to app click action
--Launch app
c:\me\MyWebApp>adb shell monkey -p com.android.webviewapp 1
Output on Logcat
Command 3
--Restore app backup
c:\me\MyWebApp>adb shell bmgr restore com.android.webviewapp
Output
restoreStarting: 1 packages
onUpdate: 0 = com.android.webviewapp
restoreFinished: 0
done
Click app manually from launcher or run the above monkey command again
Output after launch
You can launch app as many times as you want the output would still be null for Custom Application until you run the below command
Command 4
--Force close app or kill running process
c:\me\MyWebApp>adb shell am force-stop com.android.webviewapp
Click on app manually from launcher or run the above monkey command again
Output after launch
Simply put: Android OS always assumes that a backup operation is still
on-going until the app process is restarted it wont restore access to apps Custom Application class.