模仿App后台切换到前台显示闪屏(广告)页面

感觉今年,后台每一次切换到前台,都显示一次广告页面,这种实现的app越来越好,于是乎自己打算写一个


1.感觉肯定是在生命周期的哪一个步骤之中触发了这个功能,于是乎,写了一遍生命周期,权当复习了,

public class AdvActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adv);
        Log.i("TAG","onCreate");

    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("TAG","onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("TAG","onResume");

    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("TAG","onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("TAG","onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("TAG","onDestroy");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i("TAG","onRestart");

    }
}

于是乎,找到了,每次都出现的onresume 然后一想不对,这个每次都会出现,应该不合适,跳转到广告页面,广告页面结束了,切回来的时候还是会触发,onResume。
最终找到了onRestart ,测试了下
ok没问题,目前完美实现需求
贴代码

/**
 * 广告页面模仿每次显示出来广告
 * @author fxr
 */
public class AdvActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adv);
        Log.i("TAG","onCreate");

    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("TAG","onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("TAG","onResume");

    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("TAG","onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("TAG","onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("TAG","onDestroy");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i("TAG","onRestart");
        Intent intent=new Intent(AdvActivity.this,AdvTestActivity.class);
        startActivity(intent);
    }
}
public class AdvTestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adv_test);
        mHandler.sendEmptyMessageDelayed(0,3000);
    }
    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            gohome();
            super.handleMessage(msg);
        }
    };

    private void gohome() {
        Intent intent=new Intent(AdvTestActivity.this,AdvActivity.class);
        startActivity(intent);
        finish();
    }
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33413264/article/details/78400576
个人分类: android
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭