第一章 Activity专题

2018-12-21  本文已影响0人  唔笛plk

一、Activity是什么?

Activity是Android应用中最基础、最重要的应用组件,类似于Servlet与Web应用的作用,在Activity中实现视图操作,数据绑定等交互动作。
开发中,Activity有衍生出更多的子类,但在实际开发中用的还是比较少


20140210163838453.png

二、Activity的生命周期

(一)、正常生命周期

activity_lifecycle.png

对于各个生命周期的方法都有实际的使用,比如
开发地图功能就需要对应周期的管理,释放资源在onDestroy等。

(二)、异常生命周期

static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";


@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}

(三)、一些特殊情况下的生命周期分析

三、Activity的四种形态(与生命周期区分开)

activie-->pause-->stopped-->killed

四、Activity的启动模式

1.任务栈

任务是一个Activity的集合,它使用栈的方式来管理其中的Activity,这个栈又被称为返回栈(back stack);

2.为什么需要加载模式

因为Android没有提供Task提供API,无法对Task进行管理,因此添加加载模式有效控制Activity与Task之间的加载关系

3.启动模式的种类和特性

http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0520/2897.html

五、Activity组件之间的通信

1.Activity与Activity之间通讯

// 创建用于封装数据的Bundle对象
Bundle bundle = new Bundle();
bundle.putString("name", "WangJie");
bundle.putInt("age", 23);

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
//将Bundle对象嵌入Intent中
intent.putExtras(bundle);
startActivity(intent);
public class Final {
    
    public static final int REQUEST_SPOT_FRAGMENT = 9000, RESULT_SPOT_FRAGMENT = 9001;
    public static final int REQUEST_SPOT_ACTIVITY = 1000, RESULT_SPOT_ACTIVITY = 1001;
    public final static int REQUEST_GUIDE_ACTIVITY = 2000, RESULT_GUIDE_ACTIVITY = 2001;
}
public class App extends MultiDexApplication {
    /**
     * 本类实例
     */
    private static App app;
    private static RefWatcher refWatcher;
    private ThreadFactory threadFactory;
    private ExecutorService executorService;
}

2.Activity与Service之间通讯

     /**第一步Service中创建内部类
     * 返回对象给Activity
     */
    public class LocalBinder extends Binder {
        public MyService getService() {
            return MyService.this;
        }
    }
    /**第二步Service中返回内部类
     * service 回调对对象
   
     * @param intent
     * @return
     */
    @Override
    public IBinder onBind(Intent intent) {
        LogUtil.d(TAG, "onBind");
        return new LocalBinder();
    }
    /** 第三步
     * 服务绑定状态监听
     */
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            LogUtil.d(TAG, "onServiceConnected");
            try {
                localBinder = (MyService.LocalBinder) service;
                localBinder.getService().onLogin();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            LogUtil.d(TAG, "onServiceDisconnected");
        }
    };
   // 第四步
   // 启动后台服务接受一些 请求帮助、显示周围救助等的后台服务
        Intent intent = new Intent(this, MyService.class);
        intent.setPackage(this.getPackageName());
        bindService(intent, serviceConnection, Service.BIND_AUTO_CREATE);

3.Activity与Fragment之间通讯

((HomeActivity) context).onStartProgress();
((HomeActivity) context).onStopProgressError();
// Activity中
getSupportFragmentManager().beginTransaction().add(R.id.flSpotChat, spotChatFragment).commit()
// Fragment嵌套
getChildFragmentManager().beginTransaction().add(R.id.flRescueChat, rescueChatFragment).commitAllowingStateLoss();
 public interface OnSosWhoCheckListener {
        /**
         * 3:为自己呼救 1.为其他人呼救
         *
         * @param sosWho
         */
        void onCheck(String sosWho);
    }

    private OnSosWhoCheckListener onSosWhoCheckListener;

    public SpotSosDialogFragment setOnSosWhoCheckListener(OnSosWhoCheckListener onSosWhoCheckListener) {
        this.onSosWhoCheckListener = onSosWhoCheckListener;
        return this;
    }
spotSosDialogFragment = new SpotSosDialogFragment()
                .setBundle(bundle)
                .setOnSosWhoCheckListener(sosWho -> {
                    SpotFragment.this.sosWho = sosWho;
                    SpotFragment.this.sosType = "1";
                }).setOnContractListener(contractors -> {
                    SpotFragment.this.contractors = contractors;
                    SpotFragment.this.sosType = "2";
                });

4.scheme跳转协议

https://blog.csdn.net/lishuiyuntian/article/details/77477756

<activity android:name=".SchemeActivity">
            <!-- 给页面添加指定的过滤器-->
            <intent-filter>
                 <!--该页面的路径配置-->
                <data
                    android:host="test"
                    android:path="/goods"
                    android:port="8080"
                    android:scheme="qh"/>
                <!--下面这几行也必须得设置-->
                <category android:name="android.intent.category.DEFAULT"/>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.BROWSABLE"/>
            </intent-filter>
        </activity>
上一篇下一篇

猜你喜欢

热点阅读