Activity源码

来源:互联网 发布:宏编程鼠标lol算开挂吗 编辑:程序博客网 时间:2024/04/29 03:53

/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package android.app;

import android.annotation.NonNull;
import android.os.PersistableBundle;
import android.transition.Scene;
import android.transition.TransitionManager;
import android.util.ArrayMap;
import android.util.SuperNotCalledException;
import android.widget.Toolbar;

import com.android.internal.app.IVoiceInteractor;
import com.android.internal.app.WindowDecorActionBar;
import com.android.internal.app.ToolbarActionBar;
import com.android.internal.policy.PolicyManager;

import android.annotation.IntDef;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentCallbacks2;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.CursorLoader;
import android.content.IIntentSender;
import android.content.Intent;
import android.content.IntentSender;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.media.session.MediaController;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.StrictMode;
import android.os.UserHandle;
import android.text.Selection;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.method.TextKeyListener;
import android.util.AttributeSet;
import android.util.EventLog;
import android.util.Log;
import android.util.PrintWriterPrinter;
import android.util.Slog;
import android.util.SparseArray;
import android.view.ActionMode;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewManager;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManagerGlobal;
import android.view.accessibility.AccessibilityEvent;
import android.widget.AdapterView;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.HashMap;

/**
* An activity is a single, focused thing that the user can do. Almost all
* activities interact with the user, so the Activity class takes care of
* creating a window for you in which you can place your UI with
* {@link #setContentView}. While activities are often presented to the user
* as full-screen windows, they can also be used in other ways: as floating
* windows (via a theme with {@link android.R.attr#windowIsFloating} set)
* or embedded inside of another activity (using {@link ActivityGroup}).
*
* There are two methods almost all subclasses of Activity will implement:
*
*


    *
  • {@link #onCreate} is where you initialize your activity. Most
    * importantly, here you will usually call {@link #setContentView(int)}
    * with a layout resource defining your UI, and using {@link #findViewById}
    * to retrieve the widgets in that UI that you need to interact with
    * programmatically.
    *
    *
  • {@link #onPause} is where you deal with the user leaving your
    * activity. Most importantly, any changes made by the user should at this
    * point be committed (usually to the
    * {@link android.content.ContentProvider} holding the data).
    *

*
*

To be of use with {@link android.content.Context#startActivity Context.startActivity()}, all
* activity classes must have a corresponding
* {@link android.R.styleable#AndroidManifestActivity <activity>}
* declaration in their package’s AndroidManifest.xml.


*
*

Topics covered here:
*


    *
  1. Fragments
    *
  2. Activity Lifecycle
    *
  3. Configuration Changes
    *
  4. Starting Activities and Getting Results
    *
  5. Saving Persistent State
    *
  6. Permissions
    *
  7. Process Lifecycle
    *

*
*

*

Developer Guides


*

The Activity class is an important part of an application’s overall lifecycle,
* and the way activities are launched and put together is a fundamental
* part of the platform’s application model. For a detailed perspective on the structure of an
* Android application and how activities behave, please read the
* Application Fundamentals and
* Tasks and Back Stack
* developer guides.


*
*

You can also find a detailed discussion about how to create activities in the
* Activities
* developer guide.


*

*
*
*

Fragments


*
*

Starting with {@link android.os.Build.VERSION_CODES#HONEYCOMB}, Activity
* implementations can make use of the {@link Fragment} class to better
* modularize their code, build more sophisticated user interfaces for larger
* screens, and help scale their application between small and large screens.
*
*
*

Activity Lifecycle


*
*

Activities in the system are managed as an activity stack.
* When a new activity is started, it is placed on the top of the stack
* and becomes the running activity – the previous activity always remains
* below it in the stack, and will not come to the foreground again until
* the new activity exits.


*
*

An activity has essentially four states:


*

    *
  • If an activity in the foreground of the screen (at the top of
    * the stack),
    * it is active or running.
  • *
  • If an activity has lost focus but is still visible (that is, a new non-full-sized
    * or transparent activity has focus on top of your activity), it
    * is paused. A paused activity is completely alive (it
    * maintains all state and member information and remains attached to
    * the window manager), but can be killed by the system in extreme
    * low memory situations.
    *
  • If an activity is completely obscured by another activity,
    * it is stopped. It still retains all state and member information,
    * however, it is no longer visible to the user so its window is hidden
    * and it will often be killed by the system when memory is needed
    * elsewhere.
  • *
  • If an activity is paused or stopped, the system can drop the activity
    * from memory by either asking it to finish, or simply killing its
    * process. When it is displayed again to the user, it must be
    * completely restarted and restored to its previous state.
  • *

*
*

The following diagram shows the important state paths of an Activity.
* The square rectangles represent callback methods you can implement to
* perform operations when the Activity moves between states. The colored
* ovals are major states the Activity can be in.


*
*

0 0
原创粉丝点击