What is PendingIntent?
A PendingIntent
is a token that you give to a foreign application (e.g. NotificationManager
, AlarmManager
, Home Screen AppWidgetManager
, or other 3rd party applications), which allows the foreign application to use your application's permissions to execute a predefined piece of code.
If you give the foreign application an Intent, it will execute your Intent
with its own permissions. But if you give the foreign application a PendingIntent
, that application will execute your Intent
using your application's permission.
What is broadcast receiver?
It is a component which used to make system wide announcements. There are two ways to use this.
Static way
1. Declaring in manifest file
2. Extend clas broadcastreceiver
3. Override onReceive(Context context, Intent intent) - write logic which is counter action for the announcement.
4. In this approach even our application is not in running state (we are not interacting with application), we do get announcement
Dynamic way
1. Register broadcast receiver one of the life cycle method and unregister it when it is not needed.
ListenerBroadcastReceiver mReceiver = new ListenerBroadcastReceiver();
this.registerReceiver(mReceiver, filter); // lets say it is called in onCreate()
this.unregisterReceiver(mReceiver); // onDestroy()
Example for android memory leak?
class MyActivity extends Activity
{
private static Drawable sBackground;
@Override
protected void onCreate(Bundle state) {
super.onCreate(state);
TextView label = new TextView(this);
label.setText("Leaks are bad");
if (sBackground == null) {
sBackground = getDrawable(R.drawable.large_bitmap);
}
label.setBackgroundDrawable(sBackground);
setContentView(label);
}
First option, where static drawables are bound with process life cycle, which is pointing to label, which pointing to activity context. This is kind of dead lock situation where Android can't clean activity's resources because activity's pointer is held by label which can't be cleaned because it is pointed by process global data (static variables) drawable.
what is tools:context in layout xml file?
This attribute is typically set on the root element in a layout XML file, and records which activity the layout is associated with. This will for example be used by the layout editor to guess a default theme. You can use the same dot prefix as in manifests to just specify the activity class without the full application package name as a prefix.
When to use scrollview component?
When an app has layout content that might be longer than the height of the device and that content should be vertically scrollable, then we need to use Scrollview.