Follow

Replacing a Pugpig ViewController with an Android Activity

Pugpig abstracts Activities as iOS-style ViewControllers. Our ViewControllers emulate an iOS-style controller lifecycle (like calling viewDidLoad) and application lifecycle events (like simulating an ApplicationDelegate and its various callbacks like didEnterForeground).

Although our ViewControllers are derived from Activities and are easy to extend, native Android developers may be more comfortable working directly with the Android Activity class. Bypassing our ViewControllers is straightforward, but it becomes your responsibility to initialise the Pugpig libraries correctly, and to post certain lifecycle events that Pugpig requires to work properly.

Please note that you cannot mix the two approaches. You must either stick with our ViewController approach, or use Activities. If you mix approaches your application will exhibit unexpected behaviour and may be unstable.

 

1. Ensure you have the version 2.4 Pugpig aar file.

2. Remove the ApplicationDelegate meta-data line from AndroidManifest.xml, and remove the AppDelegate class from your source.

3. Re-implement your ViewControllers (usually DocumentPickerViewController and DocumentViewController) as Activities.

4. Your starting activity must initialise Pugpig. If you don't do so, your app will crash when you first make a Pugpig call. You must call

com.kaldorgroup.pugpig.app.Application.initialise(getApplicationContext())

from within your activity's "onCreate". You must do this before you make any other Pugpig call.

5. You must notify Pugpig when your application starts and stops; if you do not your app will crash or behave sluggishly, and certain parts of Pugpig (like in-app purchasing) will not work properly. To notify Pugpig that your application has started, add this to your start activity's onCreate:

NotificationCenter.postNotification(Application.DidBecomeActiveNotification, null);

In your activity's onStart, add:

NotificationCenter.postNotification(Application.WillEnterForegroundNotification, null);

and in the onStop, add:

NotificationCenter.postNotification(Application.DidEnterBackgroundNotification, null);

6. Pugpig needs to know which activity is currently active, so that it has a context to use for successfully presenting error dialogs. If you don't track the active activity, your application will crash when there is an error. You must manually update the topmost activity using the static

com.kaldorgroup.pugpig.app.Application.topActivity

We suggest you set topActivity in your onCreate and onResume, and reset it to null in your onDestroy. Note - you must ensure that you reset topActivity correctly. If you do not, it may lead to memory leaks.

7. You will need to update the OPDS feed and initialise the document list yourself. This can be done by moving the code from your application delegate into your starting activity.

8. Since you can't use our view controllers, you must find your own way of passing the required document you're opening from the DocumentPickerViewController activity to the DocumentViewController activity. Any standard Android approach will work, like extras on your Intent. Similarly you won't be able to use any of our built-in view controllers, like the ImageViewController for full-screen image preview.

 

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

Powered by Zendesk