public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
fragmentMap = new SparseArray<Fragment>(); ... }
@Override public Object instantiateItem(ViewGroup container, int position) {
Fragment instantiateItem = (Fragment) super.instantiateItem(container, position);
fragmentMap.put(position, instantiateItem); ...
return instantiateItem; }
private Fragment getNewFragmentByPosition(int i) {
// Create your fragment }
@Override public Fragment getItem(int i) {
Fragment fragment = fragmentMap.get(i);
if (fragment == null) { fragment = getNewFragmentByPosition(i); }
return fragment; }
@Override public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
fragmentMap.remove(position); }
...
}
Note that we save the fragment in the map in "instantiateItem" rather than in "getItem". This is intentional since if the activity is killed by Android and later recreated then "getItem" won't be called when the fragments are recreated, but "instantiateItem" will.
I now started using broadcast to "hear" when my user in session is null which shouldn't happen but I have worked with application where this did happen and this came handy.
You could use this very same approach when handling errors like no available connection.
Global Approach
The receiver
public class NullUserBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Do something
}
}
Session class
If I try to retrieve some value out of the session and find out that my user is null I just call this method.
private void nullUserHandler() {
// Log if you want...
Intent intent = new Intent(NullUserBroadcastReceiver.class.getName());
// The action is the same as the one defined in the manifest
intent.setAction("my.project.nulluser");
Context context = Application.getContext();
context.sendBroadcast(intent);
}
Manifest
Note: Local broadcast should be used instead. Check
Alarms can be set through the system sevice AlarmManager (context.getSystemService(Context.ALARM_SERVICE)). The problem with alarms is that once the device is shut down the alarms are erased this is why we need Android to give us time when the device has finished booting. To "ask" Android to call us after the device has finished booting we need to implement a BroadcastReceiver who receives the android.intent.action.BOOT_COMPLETED message.
In the following example I have a database with the alarms.
OnBootReceiver (receives android.intent.action.BOOT_COMPLETED messages and sets alarms)
public class OnBootReceiver extends BroadcastReceiver {
SimpleDate programStartingDate = new SimpleDate(notification.getProgramStartingDateInMillis());
String time = SimpleDate.getPrettyHour(programStartingDate.getHours(), programStartingDate.getMinutes());
String message = String.format("%s empezará a las %s en el canal %s", notification.getProgramName(), time, notification.getSignName());
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.notification_icon) .setContentTitle("ReporTV").setContentText(message);
Intent resultIntent = new Intent(context, MainActivity.class); resultIntent.putExtra(MainActivity.SHOW_ALARM_NOTIFICATION, true); resultIntent.putExtra(MainActivity.NOTIFICATION, notification);
// The stack builder object will contain an artificial back stack for the started Activity. // This ensures that navigating backward from the Activity leads out of your application to the Home screen. TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); // Adds the back stack for the Intent (but not the Intent itself)
//stackBuilder.addParentStack(MainActivity.class); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); // mId allows you to update the notification later on. mNotificationManager.notify((int) notification.getId(), mBuilder.build());
I'll create a dialog that will pop when the user is about to leave the application to confirm if that is what he/she wants.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.atention));
builder.setNegativeButton(android.R.string.cancel, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// On "Cancel" clicked functionality
}
});
builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// On "Accept" clicked functionality
}
});
builder.setMessage(getString(R.string.are_you_sure_you_want_to_quit));
AlertDialog alert = builder.create();
/* The dialog will be dismissed when the user touches outside of it */
alert.setCanceledOnTouchOutside(true);
alert.show();
If you wanted to prevent the dialog to be dismissed previous to some condition we would have to:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
...
builder.setPositiveButton(android.R.string.ok, null);
...
final AlertDialog alert = builder.create();
alert.setOnShowListener(new OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button possitiveButton = alert.getButton(AlertDialog.BUTTON_POSITIVE);
possitiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// If the conditions is fullfilled you should call: alert.dimiss();
// else do whatever you need to do
}
});
...
1) You need a Facebook account and that Facebook account has to be a Facebook developer account
2) Login with your account and enter de "Manage my applications" menu currently under "Configuration" or you can choose "Create an application" in the same menu
3) In the configuration add Android platform
4) The package name is the same that the package name in the project manifest, in the class field you have to set the class that has the "android.intent.action.MAIN"
5) In the key hashes you have to add one for the debug key and one for the release key
6) To obtain the debug key you can use the following code:
private void generateKeyHash() {
try { PackageInfo info = getPackageManager().getPackageInfo( "ar.com.fennoma.reporttv", PackageManager.GET_SIGNATURES);
8) To be able to use it with Simplefacebook (for instance) or to use it in release mode you have to disable sand mode. Currently, in the main page of the facebook application you have the application's name and to its right a circle dot filled in white or all green. If filled with white then the application is in sand mode and you have to change it to public by clicking [?] or going to "Status & Review"