-
Notifications
You must be signed in to change notification settings - Fork 1
Initialize
You implemented an application with ASAP/Shark. Your application needs an object reference to a ASAPeer or even better a SharkPeer. That’s a mandatory initialization step in every Android ASAP application. This step is required during any application launch.
Applications can also write permanent information during first initialization. You application might ask users to give themselves a name, an id or something like that. Those data can be stored with shared preferences, a database or something else.
We have two kind of initialization.
- during application installation
- during each application launch
You implemented an application with ASAP/Shark. Your application needs an object reference to a ASAPeer or even better a SharkPeer. That’s a mandatory initialization step in every Android ASAP application. This step is required during any application launch.
Applications can also write permanent information during first initialization. You application might ask users to give themselves a name, an id or something like that. Those data can be stored with shared preferences, a database or something else.
We have two kind of initialization.
Launching is system is often called bootstrepping. Those processes have the tendency to become pretty weird. You know what I mean if you ever had a look into launching code of an operating system. The following will not be that mind-blowing.
Here is the point: There is a special Activity in each Android application. It is the first Activity that is instantiated during application launch. It is defined in the manifest, like this.
<activity android:name=".sharknet.android.InitActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>Here can be a challenge. Does your application needs persistent data like a user name/id or something else. This activity needs to check if those data are already set. The next, the real first activity can be started. If not, this application is not yet initialized. It was most probably launched the very first time.
(Complete implementation can be found here).
public class InitActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
/* get persistent data e.g. from shared preference
* - throw execption if not present yet
*/
String ownerID = ...;
// data found - app was initialized before
// init your system
this.initializeSystem(ownerID);
// launch 'real' first activity
Class realFirstActivity = YourAppsFirstActivity.class;
this.finish();
Intent intent = new Intent(this, realFirstActivity);
this.startActivity(intent);
}
catch(YourException se) {
setContentView(R.layout.init);
// this init activity proceeds
}
}This algorithm is simple. We try to get some persistent information (ownerID). They are needed to initialize our system. We assume a method that get those data from a persistent memory like share preference. We also assume that this method throws an Exception an we and up in the catch clause.
We just set a layout (R.layout.init). The activity remains active. (We ask for a user name and store it in shared preferences.)
We understand this initial activity as a kind of guard. It checks if this application was properly initialized. It retrieves general information and launches an first activity that works on an initialized system.
Feel free to use this code a template for your own initial activity.