【安卓开发快速入门】 Activity开始运行和结束运行的Toast提示

实现Activity开始运行、结束运行的提示

在需要提示的Activity类中,实现registerActivityLifecycleCallbacks,代码如下(//*****************************代码开始   中间为代码    //*****************************代码结束)

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        //*****************************代码开始
        this.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks(){
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {
                count++;
                if (count == 1) {
                    Toast.makeText(activity,"Activity回到前台",Toast.LENGTH_SHORT).show();
                    Log.e(TAG, "foreground");
                }
            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {
                count--;
                if (count == 0) {
                    Toast.makeText(activity,"Activity切到前台",Toast.LENGTH_SHORT).show();
                    Log.e(TAG, "background");

                }
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
        //*****************************代码结束
    }
}
上一篇:VUE:图片上传


下一篇:Enrace的博客