Adding your own ViewController

Pugpig ViewControllers manage application lifecycle for you, providing callbacks for actions when the app starts, stops or is pause. We provide several out-of-the-box view controllers, based on their related Android Activities:

StandardViewController extends Activity
FragmentViewController extends FragmentActivity
PreferenceViewController extends PreferenceActivity

You may want to write your own ViewController based on another Activity subclass (for example, an ActionBarActivity). This guide will help you.

Most of the work is done for you in our ViewControllerHelper class. Although the class is not public, you can access it by putting your view controller into the package.

Here is an example ViewController that extends ActionBarActivity. As you can see, it's very simple, doing nothing but forwarding calls to the ViewControllerHelper.


//  Shows how to produce your own view controller.
// // import; import android.content.Context; import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle;
import; import android.view.*; public class ExampleActionBarViewController extends ActionBarActivity implements ViewController { private ViewControllerHelper helper = null; protected ExampleActionBarViewController() { super(); helper = new ViewControllerHelper(this); } protected ExampleActionBarViewController(int layoutResID) { super(); helper = new ViewControllerHelper(this, layoutResID); } public void presentViewController(ViewLauncher launcher) { helper.presentViewController(launcher); } public void dismissViewController() { helper.dismissViewController(); } public Context getContext() { return this; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); helper.onCreate(savedInstanceState); } @Override protected void onStart() { helper.onPreStart(); super.onStart(); helper.onPostStart(); } @Override protected void onRestart() { super.onRestart(); helper.onRestart(); } @Override protected void onResume() { super.onResume(); helper.onResume(); } @Override protected void onPause() { super.onPause(); helper.onPause(); } @Override protected void onStop() { helper.onPreStop(); super.onStop(); helper.onPostStop(); } @Override protected void onDestroy() { super.onDestroy(); helper.onDestroy(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); helper.onConfigurationChanged(newConfig); } @Override public void onLowMemory() { super.onLowMemory(); helper.onLowMemory(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); helper.onActivityResult(requestCode, resultCode, data); } @Override public boolean launchIntent(android.content.IntentSender intentSender, ViewController.IntentCompletionHandler handler) { return helper.launchIntent(intentSender, handler); } @Override public void onBackPressed() { super.onBackPressed(); } @Override public boolean dispatchTouchEvent(MotionEvent e) { return helper.dispatchTouchEvent(e) || super.dispatchTouchEvent(e); } public void init() { } public void viewDidLoad() { } public void viewDidUnload() { } public void viewWillAppear() { } public void viewDidAppear() { } public void viewWillDisappear() { } public void viewDidDisappear() { } public void didReceiveMemoryWarning() { } public void willRotateToInterfaceOrientation(int toOrientation) { } public void didRotateFromInterfaceOrientation(int fromOrientation) { } public ViewGroup view() { return helper.view(); } }

As the view controllers extend activities, you have full access to the standard activity functions and can override as normal. However, if you choose to override anything we implement above, please ensure you call the superclass implementation too.

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


Powered by Zendesk