显示Admob的Interestial Ad后,活动重新开始

时间:2016-08-12 03:52:43

标签: java android admob

我有这个应用程序,我正在展示Admob的Interestial Ad。它是测试应用程序,当我开始测试并开始回答问题的想法是我可以评估问题,我最后看到结果,我可以按上一个编辑我的选择。它离线工作得很好(没有广告)。但如果我激活互联网并且Interestial Ad开始出现,我将失去所有以前评估过的问题。唯一的情况是,如果在Interestial Ad弹出之前暂停测试,我就不会丢失它们。 我需要评估问题,即使广告弹出并关闭它

@Override
public void onBackPressed() {
    preDestroy();
}

private void preDestroy(){
    Log.d("TestActivity","On Stop");

    if(!canStop){
        finish();
        return;
    }
    if(!isReadonly)
        showDialog("Saving progress...");

    if(!isReadonly){
        if(!isTestMode){
            isDone = true;
        }
        if(isDone) {
            for (int x = 0; x < questions.size(); ++x) {
                Question q = questions.get(x);

                if (!isTestMode && q != null && !evaluatedQuestions.contains(q.getQuestionID())) {
                    questions.remove(x);
                    --x;
                } else if (!isReadonly && isTestMode) {
                    evaluateQuestion(true, q);
                }
            }
        }


        synchronized (TestActivity.this) {
            if (!isReadonly) {
                db.finalizeSession(sessionID, questions, isDone, currentIndex + 1, timeRemaining);
            }
        }
    }

    if(!isReadonly) {
        new CountDownTimer(800, 800) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                finish();
            }
        }.start();
    }else{
        finish();
    }
}

@Override
protected void onDestroy(){
    if(dialog!=null) {
        dialog.dismiss();
    }
    super.onDestroy();
}

@Override
protected void onResume() {
    super.onResume();

    Log.d("TestActivity","On resume");

    nextButton = (Button)findViewById(R.id.next_button);
    backButton = (Button) findViewById(R.id.back_button);
    nextButtonView = findViewById(R.id.nextBtnContainer);
    preButtonView = findViewById(R.id.backBtnContainer);
    nextButtonView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isTestMode) {
                if (currentIndex + 1 != questions.size()) {
                    showNext();
                } else {
                    finalizeSession();
                }
            } else {
                if (evaluatedQuestions.contains(getCurrentQuestion().getQuestionID())) {
                    if (currentIndex + 1 != questions.size()) {
                        showNext();
                    } else {
                        finalizeSession();
                    }
                } else {
                    QuestionFragment questionFragment = (QuestionFragment) adapter.fragments.get(currentIndex);
                    evaluateQuestion(true,questionFragment.getQuestion());
                    questionFragment.evaluateQuestionUI(true);
                }
            }
        }
    });

    preButtonView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showPrevious();
        }
    });

    if(canStop) {
        populateQuestions();
        if (questions.size() == 0) {
            Toast.makeText(this, "No questions available", Toast.LENGTH_LONG).show();
            finish();
            return;
        }


        filterQuestions();
    }else{
        canStop = true;
    }

    new CountDownTimer(500, 500) {
        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
            if(dialog!=null)
                dialog.dismiss();
        }
    }.start();
}


public class FixedSpeedScroller extends Scroller {
    private int mDuration = 1000;

    public FixedSpeedScroller(Context context){
        super(context);
    }

    public FixedSpeedScroller(Context context, Interpolator interpolator){
        super(context, interpolator);
    }

    public FixedSpeedScroller(Context context, Interpolator interpolator, boolean flywheel){
        super(context, interpolator, flywheel);
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy, int duration){
        // Ignore received duration, use fixed one instead
        if(onClickAnim) {
            Log.d("Anim","Other");
            super.startScroll(startX, startY, dx, dy, mDuration);
        }else{
            Log.d("Anim","Default");
            super.startScroll(startX, startY, dx, dy, duration);
        }
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy){
        if(onClickAnim) {
            Log.d("Anim","Other");
            super.startScroll(startX, startY, dx, dy, mDuration);
        }else{
            Log.d("Anim","Default");
            super.startScroll(startX,startY,dx,dy);
        }
    }
}
}

2 个答案:

答案 0 :(得分:0)

插页式广告是一项活动。因此,在关闭之前的活动(显示插页式广告的位置)之后,将触发onResume方法。您已在onResume方法中编码,该方法正在加载您的问题。 解决这个问题的两种方法 -

  1. 需要从onResume方法中删除问题加载代码。您可以将其放在onCreate中并将变量用作静态。
  2. 声明一个静态变量,用于跟踪插页式广告是否显示。然后检查内部onResume活动是否从插页式广告恢复,并根据您的需要进行编码。

答案 1 :(得分:0)

您需要了解Android Activity生命周期。您应该在暂停时存储Activity的状态,并在恢复时刷新它。

请参阅https://developer.android.com/training/basics/activity-lifecycle/index.html