public abstract class BaseRootActivity
extends android.app.Activity
finish()
mechanism.
A subclass of this class is expected to be set as the main activity in
AndroidManifest.xml
like the following.
<activity android:name=".RootActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
Sub classes are required to implement dispatch()
method which is
expected to launch another Activity. A simple example of a root Activity
that extends BaseRootActivity
is shown below.
public class RootActivity extends BaseRootActivity { @Override public void onCreate(Bundle savedInstance) { super.onCreate(savedInstance); setContentView(R.layout.root_activity); } @Override protected void dispatch() { // Start HomeActivity. Activities launched from here // should call App.getInstance().setStopping(true) // when they receive a key down event of KEYCODE_BACK. // Otherwise, such Activities will be re-launched by // the onResume() implementation of BaseRootActivity. // You may find BottomActivity and its variants useful. startActivity(new Intent(this, HomeActivity.class)); // Note that finish() should not be called here. // This RootActivity instance should sit at the // bottom of the Activity stack in order to benefit // from the default mechanism of Android's Activity // management. } }
The implementation of onDestroy()
method of this class
does the following to support restarting.
App
.
getInstance()
.
isRestarting()
returns true
).
Intent
with ACTION_MAIN
& CATEGORY_LAUNCHER
and pass it to Activity.startActivity(Intent)
, and the call
App
.
getInstance()
.
setRestarting
(false)
.
DEFAULT_KEYS_DIALER, DEFAULT_KEYS_DISABLE, DEFAULT_KEYS_SEARCH_GLOBAL, DEFAULT_KEYS_SEARCH_LOCAL, DEFAULT_KEYS_SHORTCUT, FOCUSED_STATE_SET, RESULT_CANCELED, RESULT_FIRST_USER, RESULT_OK
ACCESSIBILITY_SERVICE, ACCOUNT_SERVICE, ACTIVITY_SERVICE, ALARM_SERVICE, AUDIO_SERVICE, BIND_AUTO_CREATE, BIND_DEBUG_UNBIND, BIND_NOT_FOREGROUND, CLIPBOARD_SERVICE, CONNECTIVITY_SERVICE, CONTEXT_IGNORE_SECURITY, CONTEXT_INCLUDE_CODE, CONTEXT_RESTRICTED, DEVICE_POLICY_SERVICE, DROPBOX_SERVICE, INPUT_METHOD_SERVICE, KEYGUARD_SERVICE, LAYOUT_INFLATER_SERVICE, LOCATION_SERVICE, MODE_APPEND, MODE_PRIVATE, MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE, NOTIFICATION_SERVICE, POWER_SERVICE, SEARCH_SERVICE, SENSOR_SERVICE, TELEPHONY_SERVICE, UI_MODE_SERVICE, VIBRATOR_SERVICE, WALLPAPER_SERVICE, WIFI_SERVICE, WINDOW_SERVICE
Constructor and Description |
---|
BaseRootActivity() |
Modifier and Type | Method and Description |
---|---|
protected abstract void |
dispatch()
Launch another Activity.
|
protected void |
onCreate(android.os.Bundle savedInstanceState)
Called when this Activity gets started.
|
protected void |
onDestroy()
Called when this Activity gets stopped.
|
protected void |
onResume()
Called when this Activity gets resumed.
|
addContentView, closeContextMenu, closeOptionsMenu, createPendingResult, dismissDialog, dispatchKeyEvent, dispatchPopulateAccessibilityEvent, dispatchTouchEvent, dispatchTrackballEvent, findViewById, finish, finishActivity, finishActivityFromChild, finishFromChild, getApplication, getCallingActivity, getCallingPackage, getChangingConfigurations, getComponentName, getCurrentFocus, getInstanceCount, getIntent, getLastNonConfigurationInstance, getLayoutInflater, getLocalClassName, getMenuInflater, getParent, getPreferences, getRequestedOrientation, getSystemService, getTaskId, getTitle, getTitleColor, getVolumeControlStream, getWallpaperDesiredMinimumHeight, getWallpaperDesiredMinimumWidth, getWindow, getWindowManager, hasWindowFocus, isChild, isFinishing, isTaskRoot, managedQuery, moveTaskToBack, onActivityResult, onApplyThemeResource, onAttachedToWindow, onBackPressed, onChildTitleChanged, onConfigurationChanged, onContentChanged, onContextItemSelected, onContextMenuClosed, onCreateContextMenu, onCreateDescription, onCreateDialog, onCreateDialog, onCreateOptionsMenu, onCreatePanelMenu, onCreatePanelView, onCreateThumbnail, onCreateView, onDetachedFromWindow, onKeyDown, onKeyLongPress, onKeyMultiple, onKeyUp, onLowMemory, onMenuItemSelected, onMenuOpened, onNewIntent, onOptionsItemSelected, onOptionsMenuClosed, onPanelClosed, onPause, onPostCreate, onPostResume, onPrepareDialog, onPrepareDialog, onPrepareOptionsMenu, onPreparePanel, onRestart, onRestoreInstanceState, onRetainNonConfigurationInstance, onSaveInstanceState, onSearchRequested, onStart, onStop, onTitleChanged, onTouchEvent, onTrackballEvent, onUserInteraction, onUserLeaveHint, onWindowAttributesChanged, onWindowFocusChanged, openContextMenu, openOptionsMenu, overridePendingTransition, registerForContextMenu, removeDialog, requestWindowFeature, runOnUiThread, setContentView, setContentView, setContentView, setDefaultKeyMode, setFeatureDrawable, setFeatureDrawableAlpha, setFeatureDrawableResource, setFeatureDrawableUri, setIntent, setPersistent, setProgress, setProgressBarIndeterminate, setProgressBarIndeterminateVisibility, setProgressBarVisibility, setRequestedOrientation, setResult, setResult, setSecondaryProgress, setTitle, setTitle, setTitleColor, setVisible, setVolumeControlStream, showDialog, showDialog, startActivity, startActivityForResult, startActivityFromChild, startActivityIfNeeded, startIntentSender, startIntentSenderForResult, startIntentSenderFromChild, startManagingCursor, startNextMatchingActivity, startSearch, stopManagingCursor, takeKeyEvents, triggerSearch, unregisterForContextMenu
bindService, checkCallingOrSelfPermission, checkCallingOrSelfUriPermission, checkCallingPermission, checkCallingUriPermission, checkPermission, checkUriPermission, checkUriPermission, clearWallpaper, createPackageContext, databaseList, deleteDatabase, deleteFile, enforceCallingOrSelfPermission, enforceCallingOrSelfUriPermission, enforceCallingPermission, enforceCallingUriPermission, enforcePermission, enforceUriPermission, enforceUriPermission, fileList, getApplicationContext, getApplicationInfo, getAssets, getBaseContext, getCacheDir, getClassLoader, getContentResolver, getDatabasePath, getDir, getExternalCacheDir, getExternalFilesDir, getFilesDir, getFileStreamPath, getMainLooper, getPackageCodePath, getPackageManager, getPackageName, getPackageResourcePath, getResources, getSharedPreferences, getWallpaper, grantUriPermission, isRestricted, openFileInput, openFileOutput, openOrCreateDatabase, peekWallpaper, registerReceiver, registerReceiver, removeStickyBroadcast, revokeUriPermission, sendBroadcast, sendBroadcast, sendOrderedBroadcast, sendOrderedBroadcast, sendStickyBroadcast, sendStickyOrderedBroadcast, setWallpaper, setWallpaper, startInstrumentation, startService, stopService, unbindService, unregisterReceiver
protected final void onResume()
super.onResume()
.App.getInstance().
isStopping()
and if the return value is true, calls finish()
and executes
App.getInstance().
setStopping
(false)
. The finish()
call terminates
this application (if the subclass of BaseRootActivity is used as the
root Activity). The reason to call App.getInstance().setStopping(false)
is that Android may reuse this Activity instance instead of creating
a new one.dispatch()
that subclasses must implement.
Note that if a subclass of BaseRootActivity
is used as the root
Activity, the application won't terminate until App.getInstance().
setStopping
(true)
is explicitly called. You may find BaseActivity.exit()
useful to stop your application.
onResume
in class android.app.Activity
protected abstract void dispatch()
This method is called from the context of onResume()
if and
only if App.getInstance().
isStopping()
returned false when onResume()
of this Activity was called.
Implementations of this method are expected to invoke another Activity.
Note that implementations of this method should not call finish()
after invoking another Activity. It is because the purpose of the root
Activity is to sit at the bottom of the Activity stack so that this
application can benefit from the default mechanism of Android's Activity
management.
Activities launched from within dispatch()
method should call
App.getInstance().setStopping(true)
when they receive a key
down event of KEYCODE_BACK
. Otherwise, such Activities will
be re-launched by the onResume()
implementation of
BaseRootActivity
. A simple way to satisfy the requirement is
to use BottomActivity
or its variants whose onKeyDown
method calls App.getInstance().setStopping(true)
.
protected void onCreate(android.os.Bundle savedInstanceState)
This method is the best point to re-initialize the application.
In other words, this is the logical starting point of the application.
(The 'physical' starting point of the
application is onCreate()
of android.app.Application
.)
The implementation of this method emits "STARTED" log message.
onCreate
in class android.app.Activity
BaseApplication.onCreate()
protected void onDestroy()
This method is the best point to finalize the application.
In other words, this is the logical end point of the application.
(The 'physical' end point of the
application is onTerminate()
of android.app.Application
.)
The implementation of this method emits "STOPPED" log message.
And then, if required (= if App
.
getInstance()
.
isRestarting()
returns
true
), this implementation restarts this
activity by
passing an Intent
with ACTION_MAIN
& CATEGORY_LAUNCHER
to
Activity.startActivity(Intent)
. In such a case, "RESTARTING" log
message is emitted.
onDestroy
in class android.app.Activity
BaseApplication.onTerminate()
Copyright © 2016. All rights reserved.