为什么在onPause方法后的onCreate立即调用 [英] Why the onPause method is called immediately after onCreate

查看:337
本文介绍了为什么在onPause方法后的onCreate立即调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写这表明在固定时间活动的应用程序。

我开始从这样的code服务活动:

  intent.setClass(背景下,FakeAction.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(意向);
 

在FakeAction的onCreate方法我需要唤醒设备,并开始声音消息:

  getWindow()addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED)。
。getWindow()addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
。getWindow()addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
。getWindow()addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
 

有很多的code中的onCreate显示在屏幕上的信息,并启动声音信息。

下面是一个在onPause方法:

  @覆盖
保护无效的onPause()
{
    Log.i(TAG的onPause);

    //停止媒体播放器
    如果(熔点!= NULL)
    {
        如果(mp.isPlaying())mp.stop();
        mp.release();
        熔点=无效;
    }

    //恢复卷
    如果(audioManager!= NULL)
    {
        audioManager.setStreamVolume(AudioManager.STREAM_ALARM,savedVolume,0);
    }

    //停止分钟计时器
    handler.removeCallbacks(minuteReceiver);

    super.onPause();
}
 

不幸的是,在onPause()方法的onCreate后立即调用。所以,我的声音消息立即停止。

但是,如果启动该活动不被锁定时,屏幕则onPasue()不是的onCreate()后立即调用;

尽管我评论的一切getWindow()。addFlags()字符串的onPause()的onCreate()当屏幕锁定的话后调用。

现在的问题是,为什么在onPause是的onCreate后立即叫什么名字? 我如何能分辨的onPause()方法,并在onPause呼叫即时调用()当用户preSS后退按钮?

下面是活动的code。我用的是MVP模式,因此,主要code为presenter。 但是,即使我评论全部presenter的调用(就像我在这个例子中来完成的)的onPause()是的onCreate()后,立即叫

它可以有一个SENCE,我在AsyncTask的启动活动?该AsyncTask的启动形式的服务。如果完成了AsyncTask的后服务已停止。

 公共类FakeAction扩展RoboActivity
                            器物
                            View.OnClickListener
                            ,AlarmAction
{
    私有静态最后字符串变量=TA FakeAction;

    @InjectView(R.id.aa_btn_Stop)按钮btnStop;
    @InjectView(R.id.aa_btn_Snooze)按钮btnSnooze;
    @InjectView(R.id.aa_tv_CurTime)的TextView tvCurTime;
    @InjectView(R.id.aa_tv_CurDate)的TextView tvCurDate;
    @InjectView(R.id.aa_tv_AlarmName)的TextView tvAlarmName;

    @注入公共AlarmAction presenter presenter;

    私人异常恩;

    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        Log.i(TAG的onCreate);

        super.onCreate(savedInstanceState);

        。getWindow()addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        。getWindow()addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        。getWindow()addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        。getWindow()addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

        的setContentView(R.layout.alarm_action);

        尝试
        {
            //$p$psenter.onCreate(this);
        }
        赶上(例外前)
        {
            this.ex =前;
            Log.e(TAG的onCreate,前);
        }

        btnStop.setOnClickListener(本);
        btnSnooze.setOnClickListener(本);


    }

    @覆盖
    保护无效的onPause()
    {
        Log.i(TAG的onPause);

        // presenter.onPause();

        super.onPause();
    }

    @覆盖
    公共无效的onClick(视图v)
    {
        Log.i(TAG的onClick);

        // presenter.onClick(v.getId());

    }

    @覆盖
    公共捆绑getIntentBundle()
    {
        返回getIntent()getExtras()。
    }

    @覆盖
    公共无效setAlarmName(字符串alarmName)
    {
        tvAlarmName.setText(alarmName);
    }

    @覆盖
    公共字符串getAlarmName()
    {
        返回tvAlarmName.getText()的toString()。
    }

    @覆盖
    公共无效setCurTime(字符串CURTIME)
    {
        tvCurTime.setText(CURTIME);
    }

    @覆盖
    公共字符串getCurTime()
    {
        返回tvCurTime.getText()的toString()。
    }

    @覆盖
    公共无效setCurDate(字符串CURDATE)
    {
        tvCurDate.setText(CURDATE);
    }

    @覆盖
    公共字符串getCurDate()
    {
        返回tvCurDate.getText()的toString()。
    }



    @覆盖
    公共IAssetFileDescriptorMockable openFd(字符串ASSETNAME)抛出IOException异常
    {
        Log.i(TAG,openFd);
        返回新AssetFileDescriptorMockable(getAssets()openFd(ASSETNAME)。);
    }

}
 

解决方案

我发现我的问题的解决方案。

之所以叫的onPause后onCrate是ActivityManager。 在日志中,我发现行immendiately后的onCreate:活动暂停超时HistoryRecord 所以,接下来的事件happend在我的活动:

  1. 的onCreate()
  2. ONSTART()
  3. 在onResume()
  4. 活动暂停超时HistoryRecord
  5. 的onPause()
  6. 在onResume()

我不知道该怎么prevent活动暂停超时HistoryRecord,但我改变了我的申请时,开始在onResume声音消息()。即使它stopeed中的onPause()它是在下一onResume()再次启动。

I'm writing an application that show an activity at fixed time.

I start the activity from service with such code:

intent.setClass(context, FakeAction.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

in the onCreate method of FakeAction I require wake up of device and start sound message:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

There is a lot of code in onCreate to show information on screen and start the sound message.

Here is an onPause method:

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

    //Stop mediaPlayer
    if(mp != null)
    {
        if(mp.isPlaying())mp.stop();
        mp.release();
        mp = null;
    }

    //Restore volume
    if(audioManager != null)
    {
        audioManager.setStreamVolume(AudioManager.STREAM_ALARM, savedVolume, 0);
    }

    //Stop minute timer
    handler.removeCallbacks(minuteReceiver);

    super.onPause();
}

Unfortunately the onPause() method is called immediately after onCreate. So my sound message is immediately stopped.

But if the activity is started when the screen is not locked then the onPasue() is not called immediately after onCreate();

Even though I comment all of "getWindow().addFlags()" strings the onPause() is called after onCreate() when the screen if locked.

The question is why onPause is called immediately after onCreate? How can I distinguish immediate call of onPause() method and call of onPause() when user press the back button?

Below is the code of the activity. I use the MVP pattern, so, the main code is in presenter. But even if I comment all presenter's calls (like I done in this example) the onPause() is called immediately after onCreate()

Can it have a sence that I start activity in AsyncTask? The AsyncTask is started form service. The service is stopped after the AsyncTask if finished.

public class FakeAction extends RoboActivity
                            implements 
                            View.OnClickListener
                            ,AlarmAction
{
    private static final String TAG = "TA FakeAction";

    @InjectView(R.id.aa_btn_Stop)       Button btnStop;
    @InjectView(R.id.aa_btn_Snooze)     Button btnSnooze;
    @InjectView(R.id.aa_tv_CurTime)     TextView tvCurTime;
    @InjectView(R.id.aa_tv_CurDate)     TextView tvCurDate;
    @InjectView(R.id.aa_tv_AlarmName)   TextView tvAlarmName;

    @Inject public AlarmActionPresenter presenter;

    private Exception   ex;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        Log.i(TAG, "onCreate");

        super.onCreate(savedInstanceState);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

        setContentView(R.layout.alarm_action);

        try
        {
            //presenter.onCreate(this);
        }
        catch(Exception ex)
        {
            this.ex = ex;
            Log.e(TAG, "onCreate",  ex);
        }

        btnStop.setOnClickListener(this);
        btnSnooze.setOnClickListener(this);


    }

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

        //presenter.onPause();

        super.onPause();
    }

    @Override
    public void onClick(View v)
    {
        Log.i(TAG, "onClick");

        //presenter.onClick(v.getId());

    }

    @Override
    public Bundle getIntentBundle()
    {
        return getIntent().getExtras();
    }

    @Override
    public void setAlarmName(String alarmName)
    {
        tvAlarmName.setText(alarmName);
    }

    @Override
    public String getAlarmName()
    {
        return tvAlarmName.getText().toString();
    }

    @Override
    public void setCurTime(String curTime)
    {
        tvCurTime.setText(curTime);
    }

    @Override
    public String getCurTime()
    {
        return tvCurTime.getText().toString();
    }

    @Override
    public void setCurDate(String curDate)
    {
        tvCurDate.setText(curDate);
    }

    @Override
    public String getCurDate()
    {
        return tvCurDate.getText().toString();
    }



    @Override
    public IAssetFileDescriptorMockable openFd(String assetName) throws IOException
    {
        Log.i(TAG, "openFd");
        return new AssetFileDescriptorMockable(getAssets().openFd(assetName));
    }

}

解决方案

I found out the solution of my problem.

The reason why the onPause called after onCrate is the ActivityManager. In the log I've found the line immendiately after onCreate: "Activity pause timeout for HistoryRecord" So, next events happend in my Activity:

  1. onCreate()
  2. onStart()
  3. onResume()
  4. Activity pause timeout for HistoryRecord
  5. onPause()
  6. onResume()

I don't know how to prevent the "Activity pause timeout for HistoryRecord" but I changed my application to it start the sound message in onResume(). Even though it is stopeed in onPause() it is started again in next onResume().

这篇关于为什么在onPause方法后的onCreate立即调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆