Chapter AT2
Chapter AT2
onAttach() It is called when the fragment has been associated with an activity.
onActivityCreated() It is called when the fragment activity has been created and the fragment view
hierarchy instantiated.
onPause() It is called when fragment is no longer visible and it indicates that the user is
leaving the fragment.
onDestoryView() The view hierarchy which associated with the fragment is being removed after
executing this method.
onDetach() It is called immediately after the fragment disassociated from the activity.
Intents
Android uses Intent for communicating between
the components (such as activities, services,
broadcast receivers and content providers) of an
Application and also from one application to
another application.
It helps you to redirect your activity to another
activity on occurrence of any event
For example startActivity() you can perform this
task.
Intent intent = new Intent(getApplicationContext(),
SecondActivity.class);
startActivity(intent);
foreground activity is getting redirected to another
activity i.e. SecondActivity.java.
getApplicationContext() returns the context for your
foreground activity
Types of Intents
Explicit Intent:
Explicit Intents are used to connect the application internally.
In Explicit we use the name of component which will be affected by
Intent
Explicit Intent works internally within an application to perform
navigation and data transfer. Example:
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
Here SecondActivity is the JAVA class name where the activity will now
be navigated.
Implicit Intent:
In Implicit Intents we do need to specify the name of the component.
We just specify the Action which has to be performed and further this
action is handled by the component of another application. The basic
example of implicit Intent is to open any web page.
Intent intentObj = new Intent(Intent.ACTION_VIEW);
intentObj.setData(Uri.parse("https://www.google.com"));
startActivity(intentObj);
Unlike Explicit Intent you do not use any class name to pass through
Intent().
Example
Benefits of Intents
For activity: Intent object helps to start a new
activity and passing data to the second activity
For Services: Services work in background,
Intents could be used to start a Service
For Broadcast Receivers: Android system
initiates some broadcast message on several
events, such as System Reboot, Low Battery
warning message etc.
For Android Applications: Whenever you need
to navigate to another activity of your app or you
need to send some information to next activity
then we can always prefer to Intents for doing so.
Services
Android service is a component that is used to perform long running
operations on the background such as playing music, handle network
transactions, interacting with content providers etc.
It doesn't have any UI (user interface)
service can be bounded by a component to perform interactivity and
inter process communication (IPC).
Life Cycle of Android Service
1) Started Service (foreground or background)
A service is started when component (like activity) calls
startService() method, now it runs in the background indefinitely.
It runs in the background even if the application that started the
service is closed.
It is stopped by stopService() method.
The service can stop itself by calling the stopSelf() method.
2) Bound Service
A service is bound when another component (e.g. client) calls
bindService() method.
The client can unbind the service by calling the unbindService()
method. The service cannot be stopped until all clients unbind the
service.
Menu
Menu is a part of user interface (UI) component which is used
to handle some common functionality around the application
useful for displaying additional options that are not directly
visible on the main UI of an application
Menu can be defined in separate XML file and use that file in
our activities or fragments based on our requirements.
Define android menu
create a new folder menu inside of our project resource directory
(res/menu/) and add a new XML file to build the menu
Use menu, item and group(Optional) xml tags
You can also create sub menu
<item>
<menu><item></item></menu>
</item>
Use MenuInflater.inflate() to load the menu from an activity
and define an event handler to inform what to perform when the
menu is selected
Creating Menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/inbox“ android:icon="@drawable/ic_inbox"
android:title="@string/inbox" />
@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Inbox") { // do your
coding }
else { return false; }
return true;
Popup Menu
displays a list of items in a vertical list that’s
anchored to the view that invoked the menu
It does not support any item shortcuts and item
icons
Popup menu is available with API level 11
(Android 3.0) and higher versions
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Popup Menu"
android:layout_marginTop="200dp" android:layout_marginLeft="1
00dp"/>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/
android" >
<item android:id="@+id/inbox_item"
android:title="Inbox" />
<item android:id="@+id/compose_item"
android:title="Compose" />
<item android:id="@+id/outbox_item"
android:title="Outbox" />
<item android:id="@+id/spam_item"
android:title="Spam" />
<item android:id="@+id/logout_item"
android:title="Logout" />
</menu>
public
class MainActivity extends AppCompatActivity implements PopupMenu.O
nMenuItemClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btnShow);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(MainActivity.this, v);
popup.setOnMenuItemClickListener(MainActivity.this);
popup.inflate(R.menu.popup_menu);
popup.show();
}
});
}
@Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(this, "Selected Item: " +item.getTitle(),
Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.inbox_item:
// do your code
return true;
case R.id.compose_item:
// do your code
return true;
case R.id.outbox_item:
// do your code
return true;
case R.id.spam_item:
// do your code
return true;
case R.id.logout_item:
// do your code
return true;
default:
return false;
} } }