I am working on a alarm application. The structure is somehow complex. It works like that:
The main activity is a FragmentTabhost activity, it has 4 tabs, one of the fragment is the alarm list.
So, the alarm is trigger by alarmManager, it first call the boardcast receiver , in the receiver there is a service , that start a new activity, showing the detail info. of the alarm.
And that is it. What I would like to do is when the user onbackpress that new activity, I would like to update the alarm list in that fragment. How to achieve that? Thanks for helping
Update : Attempt use Boardcast receiver but it does not receive
in the Alarm Activity
@Override
public void onBackPressed() {
Log.d("test1","test R1");
Intent intent = new Intent("on_alarm_remove");
LocalBroadcastManager.getInstance(ctx).sendBroadcast(intent);
super.onBackPressed();
}
in the Alarm tab Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ctx = getActivity();
intentFilter = new IntentFilter("on_alarm_remove");
LocalBroadcastManager.getInstance(ctx).registerReceiver(mMessageReceiver, intentFilter);
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("test1","test R");
db.removeRecord(db.getFirstAlarm().alarm_id);
reloadFragment();
}
};
@Override
public void onPause() {
if (mMessageReceiver != null)
LocalBroadcastManager.getInstance(ctx).unregisterReceiver(mMessageReceiver);
super.onPause();
}
In manifest
<receiver android:name=".reminder.ReminderFrag$mMessageReceiver" />
It doesn't crash, but nothing happen, In the log "test R1" has run but "test R" does not. Thanks again for helping
Assuming this only needed if the fragment is present, then you can get the fragment by id in the activity:
public void onBackPressed() {
Fragment f = getFragmentManager().findFragmentById( ... ); // Fragment's Id
if (f.getClass() == AlarmFragment.class) {
((AlarmFragment)f).removeRecord();
return;
}
super.onBackPressed();
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments