Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy the onPause method is called immediately after onCreate
    primarykey
    data
    text
    <p>I'm writing an application that show an activity at fixed time.</p> <p>I start the activity from service with such code:</p> <pre><code>intent.setClass(context, FakeAction.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); </code></pre> <p>in the onCreate method of FakeAction I require wake up of device and start sound message:</p> <pre><code>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></pre> <p>There is a lot of code in onCreate to show information on screen and start the sound message.</p> <p>Here is an onPause method:</p> <pre><code>@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(); } </code></pre> <p>Unfortunately the onPause() method is called immediately after onCreate. So my sound message is immediately stopped.</p> <p>But if the activity is started when the screen is not locked then the onPasue() is not called immediately after onCreate();</p> <p>Even though I comment all of "getWindow().addFlags()" strings the onPause() is called after onCreate() when the screen if locked.</p> <p>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?</p> <p>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()</p> <p>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.</p> <pre><code>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)); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload