精华内容
下载资源
问答
  • Activity生命周期

    2016-07-21 15:20:53
    Activity生命周期

    Activity生命周期


    展开全文
  • activity生命周期

    2014-10-14 13:44:47
    activity生命周期

    马上要去面试了,心里有点怕怕,在家把基础知识复习一遍,今天说说activity生命周期,其实 面试也没什么人讲,也不愿意写,但还是硬着头皮写下来,新建一个android项目:activitylife,依次把生命周期方法打印分析下就哦了

    package com.example.activitylife;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    public class MainActivity extends Activity {
    	private static final String TAG = "MainActivity";
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		Log.e(TAG,"onCreate------------>");
    	}
    
    	@Override
    	protected void onStart() {
    		super.onStart();
    		Log.e(TAG,"onStart------------>");
    	}
    
    	@Override
    	protected void onRestart() {
    		super.onRestart();
    		Log.e(TAG,"onRestart------------>");
    	}
    
    	@Override
    	protected void onResume() {
    		super.onResume();
    		Log.e(TAG,"onResume------------>");
    	}
    
    	@Override
    	protected void onPause() {
    		super.onPause();
    		Log.e(TAG,"onPause------------>");
    	}
    
    	@Override
    	protected void onStop() {
    		super.onStop();
    		Log.e(TAG,"onStop------------>");
    	}
    
    	@Override
    	protected void onDestroy() {
    		super.onDestroy();
    		Log.e(TAG,"onDestroy------------>");
    	}
    }
    

    当项目运行的时候 log:


    onStart():可见但没焦点

    onResume():可见有焦点就意味着用户可以在屏幕上操作

    这个时候如果屏幕被盖住了 log信息:



    onPause()和onresume()方法是相对的,它是界面没有焦点了,

    onStop()屏幕不可见,

    但是这时候activity并没有消失,而是在任务栈中,这个时候我们把电话挂了,long信息:


    我们看到这个时候并没有执行onCreate()方法,而是执行了onRestart(),onStart(),onResume(),

    这个时候按后退键log信息:


    只有调用了onDestory()方法,activity才会消失,

    那么在实际开发中如何使用呢?举例说明

    1):记得在做项目中有一个用户余额界面,因为一进来就要显示用户的余额,还提供了支付宝充值功能,当用户充值成功后返回到此界面,这个余额肯定是要变的,如果获取用户余额的接口在onCreate()中调用,那么在充值成功后返回时,不是最新的,因此刚开始获取用户的余额不能放在onCreate()方法中,就需要放在onResume()中进行,当支付宝充值成功后返回也会执行onResume()方法,这就是一个例子

    2):比如当前有一个音乐播放,这个时候来了一个电话,当电话挂了以后,要从上次播放的位置进行播放,那么就要记录音乐播放到哪里,那么就要保留一个position这个时候再那保存比较好了,如果activity生命周期看懂了,就懂了,在onPause或者onStop方法都行

    今天讲到此

    展开全文
  • Activity 生命周期

    2014-05-28 19:30:14
    Managing the Activity Lifecycle ...Activity生命周期的管理  As a user navigates through, out of, and back to your app, the Activity instances in your app transition between different states in their li
    Managing the Activity Lifecycle
    Activity生命周期的管理
            As a user navigates through, out of, and back to your app, the Activity instances in your app transition between different states in their lifecycle. For instance, when your activity starts for the first time, it comes to the foreground of the system and receives user focus. During this process, the Android system calls a series of lifecycle methods on the activity in which you set up the user interface and other components. If the user performs an action that starts another activity or switches to another app, the system calls another set of lifecycle methods on your activity as it moves into the background (where the activity is no longer visible, but the instance and its state remains intact).
             用户通过导航,走出,回到你的应用程序,应用程序中的Activity实例生命周期的不同状态之间的过渡。例如,当你的Activity开始第一次系统的前景和接收用户的焦点。在这个过程中,Android系统调用一系列的生命周期方法的Activity设置用户界面和其他组件。如果用户执行一个动作,开始另一个Activity或切换到另一个应用程序,系统调用另一组生命周期方法在你的Activity,因为它进入后台(Activity不再是可见的,但实例及其状态保持不变)。
           Within the lifecycle callback methods, you can declare how your activity behaves when the user leaves and re-enters the activity. For example, if you're building a streaming video player, you might pause the video and terminate the network connection when the user switches to another app. When the user returns, you can reconnect to the network and allow the user to resume the video from the same spot.
           在生命周期回调方法中,您可以声明你的Activity行为当用户叶子和re-enters Activity。例如,如果您正在构建一个流媒体视频播放器,你可能会暂停视频和终止网络连接,当用户切换到另一个应用程序。当用户返回时,你可以重新连接到网络,允许用户从同一地点恢复视频。
           This class explains important lifecycle callback methods that each Activity instance receives and how you can use them so your activity does what the user expects and does not consume system resources when your activity doesn't need them.

           这类解释了重要的生命周期回调方法,每个Activity实例接收和如何使用它们,这样你的Activity用户的期望,不消耗系统资源当你的Activity不需要他们。


    Starting an Activity
    启动一个Activity
        Learn the basics about the activity lifecycle, how the user can launch your app, and how to perform basic activity creation.
        基本了解Activity的生命周期,用户可以启动应用程序,以及如何执行基本的创造Activity。


    Pausing and Resuming an Activity
    暂停和恢复活动
        Learn what happens when your activity is paused (partially obscured) and resumed and what you should do during these state changes.
        基本了解Activity的生命周期,用户可以启动应用程序,以及如何执行基本的创造Activity。


    Stopping and Restarting an Activity
    停止和重新启动一个活动
        Learn what happens when the user completely leaves your activity and returns to it.
    了解当用户完全离开你的Activity并返回它。


    Recreating an Activity
    重新创建一个Starting an Activity
        Learn what happens when your activity is destroyed and how you can rebuild the activity state when necessary.
    了解会发生什么当你的活动是摧毁,必要时如何重建Activity状态。


       

    展开全文

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 22,566
精华内容 9,026
关键字:

activity生命周期