I am making alarm application. I have 3 Activities: Main
, Alarm
,Game
.
They are declared in manifest:
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize"
android:screenOrientation="sensorPortrait"
>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".Alarm"
android:excludeFromRecents="true"
android:launchMode="singleTask"
android:screenOrientation="sensorPortrait"
/>
<activity
android:name=".Game"
android:excludeFromRecents="true"
android:label="@string/search_word"
android:launchMode="singleTask"
android:screenOrientation="sensorPortrait"
/>
Alarm
activity is called from broadcastreciever:
Intent i = new Intent(context, NotificationActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Or from `Game's onBackPressed:
if (!test) {
Intent i = new Intent(mContext, NotificationActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
finish();
Game
activity is called from Alarm activity in similar way:
i = new Intent(this, BubblePopActivity.class);
i.putExtras(b);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
There is no internal calls to Main
activity.
Behaiviour I am tring to achieve:
1) Activities Alarm
and Game
should be shown over lockscreen both unlocked nad locked with pin, grafical pattern and so on. It works with
flags decrared in activities, which requires FLAG_NEW_TASK in intent
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_bgame);
2) Back button should lead from Game
activity to Alarm
activity
3) If home
is pressed, while Alarm
or Game
activity is on top of stack, launcher icon should reopen them, but not the Main
, as long as those activities are not finished by application logic.
While 1) and 2) works flowlesly, 3) works occasionnaly. Sometimes it returns to Game
of Alarm
activity as it should. It looks like if those activities were killed by system due to lack of resourses, launcher icon will lead to Main
activity.
How can I implement desired logic?
I added sharede preference constants. When I launch main activity, I check if alarm was dissmissed by user, then launch as normal, else launch alarm activity.
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean serviceIsRunning = isMyServiceRunning(AlarmService.class);
if (serviceIsRunning) {
boolean alarmRings = RingingAlarmUtil.getRingingAlarm(this).isExpired();
if (!alarmRings) {
Intent i = new Intent(this, NotificationActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
}
}
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
//normal launch staff
}