Issue
I would like to run a bunch of tests in my Android App. The thing is: I want to define first a number of tests N, and make my App run that number of times, one after another.
It's a little tricky in Android, because of the Activities lifecycle, but the goal is to start a test (execute the app again) right after when the last one stoped.
Is that possible? I tried reading the Android Developers Testing section, but I'm having some doubts about if what I want is possible with that technique.
Plus, I want to make each test execute with different values for the variables (different inputs), but that's probably even more tricky, so... let's focus in the first problem :)
Any help?? Thanks
Solution
It sounds to me what you want is a mechanism to restart your Android application programatically (and gracefully). Many people may say it is impossible, but you can implement the mechanism.
The basic flow is:
(1) finish() your root activity.
(2) In onDestroy() of your root activity, call startActivity(createMainLauncherIntent()).
(3) And the implementation of createMainLauncherIntent() should look like the following.
private Intent createMainLauncherIntent()
{
Intent intent = new Intent();
// To launch this activity as if it started from the launcher.
intent.setClass(this, getClass());
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
return intent;
}
(4) Of course, the onDestroy() should have a mechanism to avoid infinite loop of 'restart'.
A sample of base root Activity class:
https://github.com/TakahikoKawasaki/nv-android-base/blob/master/src/main/java/com/neovisionaries/android/app/BaseRootActivity.java
A sample Android application that implements 'restart' mechanism:
https://github.com/TakahikoKawasaki/nv-android-base-sample
Answered By - Takahiko Kawasaki
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.